From 7a4a6ed0e8bf64e324890752dc3ed32d8f59da92 Mon Sep 17 00:00:00 2001 From: LiuYuanchi Date: Sun, 5 May 2024 19:09:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=A1=B9=E7=9B=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/compile-grammar.iml | 8 + .idea/encodings.xml | 6 + .idea/modules.xml | 8 + .idea/workspace.xml | 48 +++ LL1.cpp | 351 ++++++++++++++++ LL1.h | 32 ++ RCa19240 | Bin 0 -> 2794 bytes compare.py | 23 ++ compile-grammar.sln | 31 ++ compile-grammar.vcxproj | 149 +++++++ compile-grammar.vcxproj.filters | 48 +++ dfa.cpp | 251 ++++++++++++ grammar.cpp | 520 ++++++++++++++++++++++++ grammar.h | 55 +++ grammar.txt | 70 ++++ main.cpp | 79 ++++ nfa.cpp | 253 ++++++++++++ nfa.h | 173 ++++++++ nfa.txt | 56 +++ tests/00/00.txt | 3 + tests/00/00_grammar.txt | 34 ++ tests/00/00_lexical.txt | 9 + tests/00/00_main.ll | 23 ++ tests/00/00_my_grammar.txt | 34 ++ tests/00/00_my_lexical.txt | 9 + tests/00/00_my_lexical1.txt | 9 + tests/00/grammar_diff.html | 83 ++++ tests/00/lexical_diff.html | 58 +++ tests/01/01.txt | 7 + tests/01/01_grammar.txt | 119 ++++++ tests/01/01_lexical.txt | 26 ++ tests/01/01_my_grammar.txt | 119 ++++++ tests/01/01_my_lexical.txt | 26 ++ tests/01/01_var_defn2.ll | 30 ++ tests/01/grammar_diff.html | 168 ++++++++ tests/01/lexical_diff.html | 75 ++++ tests/02/02.txt | 7 + tests/02/02_grammar.txt | 151 +++++++ tests/02/02_lexical.txt | 30 ++ tests/02/02_my_grammar.txt | 151 +++++++ tests/02/02_my_lexical.txt | 30 ++ tests/02/02_var_defn3.ll | 35 ++ tests/02/grammar_diff.html | 200 +++++++++ tests/02/lexical_diff.html | 79 ++++ tests/07/07.txt | 4 + tests/07/07_const_var_defn3.ll | 23 ++ tests/07/07_grammar.txt | 80 ++++ tests/07/07_lexical.txt | 19 + tests/07/07_my_grammar.txt | 80 ++++ tests/07/07_my_lexical.txt | 19 + tests/07/grammar_diff.html | 129 ++++++ tests/07/lexical_diff.html | 68 ++++ tests/08_编译错误示例/08.txt | 7 + tests/08_编译错误示例/08_grammar.txt | 28 ++ tests/08_编译错误示例/08_lexical.txt | 26 ++ tests/08_编译错误示例/08_my_grammar.txt | 28 ++ tests/08_编译错误示例/08_my_lexical.txt | 28 ++ tests/08_编译错误示例/grammar_diff.html | 77 ++++ tests/08_编译错误示例/lexical_diff.html | 77 ++++ tests/10_编译错误示例/10.txt | 6 + tests/10_编译错误示例/10_grammar.txt | 26 ++ tests/10_编译错误示例/10_lexical.txt | 19 + tests/10_编译错误示例/10_my_grammar.txt | 21 + tests/10_编译错误示例/10_my_lexical.txt | 19 + tests/10_编译错误示例/grammar_diff.html | 75 ++++ tests/10_编译错误示例/lexical_diff.html | 68 ++++ tool.cpp | 287 +++++++++++++ 67 files changed, 4890 insertions(+) create mode 100644 .idea/compile-grammar.iml create mode 100644 .idea/encodings.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/workspace.xml create mode 100644 LL1.cpp create mode 100644 LL1.h create mode 100644 RCa19240 create mode 100644 compare.py create mode 100644 compile-grammar.sln create mode 100644 compile-grammar.vcxproj create mode 100644 compile-grammar.vcxproj.filters create mode 100644 dfa.cpp create mode 100644 grammar.cpp create mode 100644 grammar.h create mode 100644 grammar.txt create mode 100644 main.cpp create mode 100644 nfa.cpp create mode 100644 nfa.h create mode 100644 nfa.txt create mode 100644 tests/00/00.txt create mode 100644 tests/00/00_grammar.txt create mode 100644 tests/00/00_lexical.txt create mode 100644 tests/00/00_main.ll create mode 100644 tests/00/00_my_grammar.txt create mode 100644 tests/00/00_my_lexical.txt create mode 100644 tests/00/00_my_lexical1.txt create mode 100644 tests/00/grammar_diff.html create mode 100644 tests/00/lexical_diff.html create mode 100644 tests/01/01.txt create mode 100644 tests/01/01_grammar.txt create mode 100644 tests/01/01_lexical.txt create mode 100644 tests/01/01_my_grammar.txt create mode 100644 tests/01/01_my_lexical.txt create mode 100644 tests/01/01_var_defn2.ll create mode 100644 tests/01/grammar_diff.html create mode 100644 tests/01/lexical_diff.html create mode 100644 tests/02/02.txt create mode 100644 tests/02/02_grammar.txt create mode 100644 tests/02/02_lexical.txt create mode 100644 tests/02/02_my_grammar.txt create mode 100644 tests/02/02_my_lexical.txt create mode 100644 tests/02/02_var_defn3.ll create mode 100644 tests/02/grammar_diff.html create mode 100644 tests/02/lexical_diff.html create mode 100644 tests/07/07.txt create mode 100644 tests/07/07_const_var_defn3.ll create mode 100644 tests/07/07_grammar.txt create mode 100644 tests/07/07_lexical.txt create mode 100644 tests/07/07_my_grammar.txt create mode 100644 tests/07/07_my_lexical.txt create mode 100644 tests/07/grammar_diff.html create mode 100644 tests/07/lexical_diff.html create mode 100644 tests/08_编译错误示例/08.txt create mode 100644 tests/08_编译错误示例/08_grammar.txt create mode 100644 tests/08_编译错误示例/08_lexical.txt create mode 100644 tests/08_编译错误示例/08_my_grammar.txt create mode 100644 tests/08_编译错误示例/08_my_lexical.txt create mode 100644 tests/08_编译错误示例/grammar_diff.html create mode 100644 tests/08_编译错误示例/lexical_diff.html create mode 100644 tests/10_编译错误示例/10.txt create mode 100644 tests/10_编译错误示例/10_grammar.txt create mode 100644 tests/10_编译错误示例/10_lexical.txt create mode 100644 tests/10_编译错误示例/10_my_grammar.txt create mode 100644 tests/10_编译错误示例/10_my_lexical.txt create mode 100644 tests/10_编译错误示例/grammar_diff.html create mode 100644 tests/10_编译错误示例/lexical_diff.html create mode 100644 tool.cpp diff --git a/.idea/compile-grammar.iml b/.idea/compile-grammar.iml new file mode 100644 index 0000000..bc2cd87 --- /dev/null +++ b/.idea/compile-grammar.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..1b5ce4a --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..bf69208 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..0d8ef12 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,48 @@ + + + + + + + + + + + + + + + + + + + + + 1685010054732 + + + + + + \ No newline at end of file diff --git a/LL1.cpp b/LL1.cpp new file mode 100644 index 0000000..8d1f4c4 --- /dev/null +++ b/LL1.cpp @@ -0,0 +1,351 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "LL1.h" + + +LL1::LL1() +{ + read_grammar(); + init_grammar_set(); +} + +LL1::~LL1() +{ +} + +bool LL1::IsLL1() +{ + string symbol; + vector right_first = vector(); + vector left_follow; + for (int i = 0; i < grammar_rules.size(); i++) { + symbol.clear(); + right_first.clear(); + left_follow.clear(); + + symbol = grammar_rules[i].first; + + + + // ʽ FOLLOW + left_follow = follow[symbol]; + + // ʽҲ FIRST + + // X1 $ + for (int j = 0; j < first[grammar_rules[i].second[0]].size(); j++) { + if (first[grammar_rules[i].second[0]][j] == "$") { + continue; + } + right_first.push_back(first[grammar_rules[i].second[0]][j]); + } + + int cnt; + for (cnt = 1; cnt < grammar_rules[i].second.size(); cnt++) { + + // ҽ $ ʱ + if (!infer_empty[grammar_rules[i].second[cnt - 1]]) { + break; + } + for (int j = 0; j < first[grammar_rules[i].second[cnt]].size(); j++) { + if (first[grammar_rules[i].second[cnt]][j] == "$") { + continue; + } + right_first.push_back(first[grammar_rules[i].second[cnt]][j]); + } + } + + // Ƶ $ ʱ + if (cnt == grammar_rules[i].second.size() && infer_empty[grammar_rules[i].second[0]]) { + right_first.push_back("$"); + } + + // ԲʽҲ FIRST ȥ + set sright_first(right_first.begin(), right_first.end()); + right_first.clear(); + right_first.resize(sright_first.size()); + right_first.assign(sright_first.begin(), sright_first.end()); + + + + vector symbol_select; + + // ʽҲ FIRST Ϊ {$} ʱ + if (right_first.size() == 1 && right_first[0] == "$") { + + // SELECT Ϊ ʽҲ FOLLOW {$} Ľ + symbol_select = left_follow; + if (find(left_follow.begin(), left_follow.end(), "$") == left_follow.end()) { + symbol_select.push_back("$"); + } + } + else + { + // SELECT Ϊ ʽ FIRST + symbol_select = right_first; + } + + // SELECT м + sort(symbol_select.begin(), symbol_select.end()); + + vector new_select = vector(); + + // ж SELECT + if (select.find(symbol) == select.end()) { + + select[symbol] = symbol_select; + } + else { + + // жͬʽ SELECT Ƿཻ + set_intersection(symbol_select.begin(), symbol_select.end(), select[symbol].begin(), select[symbol].end(), back_inserter(new_select)); + + if (new_select.size() == 0) { + // ཻ㣬߲ + set_union(symbol_select.begin(), symbol_select.end(), select[symbol].begin(), select[symbol].end(), back_inserter(new_select)); + } + else + { + // LL(1) ķ˳ + cout << "ķΪ LL(1) ķ" << endl; + return false; + } + + } + + } + + // cout << "ķΪ LL(1) ķ" << endl; + return true; +} + +void LL1::build_LL1_predict() +{ + // ÿһ ս гʼ + for (int i = 0; i < VNs.size(); i++) { + if (LL1_predict.find(VNs[i]) == LL1_predict.end()) { + LL1_predict[VNs[i]] = unordered_map(); + } + } + + string symbol; + vector right_first = vector(); + vector left_follow; + + // ʽ Ԥ + for (int i = 0; i < grammar_rules.size(); i++) { + symbol.clear(); + right_first.clear(); + left_follow.clear(); + + symbol = grammar_rules[i].first; + + + // ʽ FOLLOW + left_follow = follow[symbol]; + + unordered_map &symbol_predict = LL1_predict[symbol]; + + + // ʽҲ FIRST + + // X1 $ + for (int j = 0; j < first[grammar_rules[i].second[0]].size(); j++) { + if (first[grammar_rules[i].second[0]][j] == "$") { + continue; + } + right_first.push_back(first[grammar_rules[i].second[0]][j]); + } + + int cnt; + for (cnt = 1; cnt < grammar_rules[i].second.size(); cnt++) { + + // ҽ $ ʱ + if (!infer_empty[grammar_rules[i].second[cnt - 1]]) { + break; + } + for (int j = 0; j < first[grammar_rules[i].second[cnt]].size(); j++) { + if (first[grammar_rules[i].second[cnt]][j] == "$") { + continue; + } + right_first.push_back(first[grammar_rules[i].second[cnt]][j]); + } + } + + // Ƶ $ ʱ + if (cnt == grammar_rules[i].second.size() && infer_empty[grammar_rules[i].second[0]]) { + right_first.push_back("$"); + } + + // ԲʽҲ FIRST ȥ + set sright_first(right_first.begin(), right_first.end()); + right_first.clear(); + right_first.resize(sright_first.size()); + right_first.assign(sright_first.begin(), sright_first.end()); + + // ѭ FIRST гʼ + for (int j = 0; j < right_first.size(); j++) { + if (right_first[j] == "$") { + pair> new_rule (grammar_rules[i].first, vector()); + new_rule.second.push_back("$"); + int rule_id = insert_rule(new_rule); + + for (int k = 0; k < left_follow.size(); k++) { + symbol_predict[left_follow[k]] = rule_id; + } + } + symbol_predict[right_first[j]] = i; + + } + + } + + +} + +void LL1::print_LL1_predict() +{ + cout << "[LL1_predict]:" << endl; + for (auto iter = LL1_predict.begin(); iter != LL1_predict.end(); ++iter) { + cout << (*iter).first << " "; + for (auto j = (*iter).second.begin(); j != (*iter).second.end(); ++j) { + cout << (*j).first << "," << (*j).second << " "; + } + cout << endl; + + } + cout << endl << endl; + +} + +void LL1::build_LL1_grammar() +{ + // ջ + stack stack; + int token_cnt = 0; + + // ʼ ջ + stack.push(start); + + while (!stack.empty()) + { + LL1_grammar_log.push_back(string()); + + // ջ + // жջǷΪ շ + if (stack.top() == "$") { + // ջ EOF ʾ + LL1_grammar_log.back() += "EOF"; + } + else + { + LL1_grammar_log.back() += stack.top(); + } + + // # ָ + LL1_grammar_log.back() += "#"; + + // ķ + string this_token; + if (token_cnt == token_strings.size()) { + // ջ EOF ʾ + this_token = "$"; + LL1_grammar_log.back() += "EOF"; + } + else + { + this_token = token_strings[token_cnt]; + LL1_grammar_log.back() += token_strings[token_cnt]; + } + + // ջԪ뼴ķŽбȽ + if (stack.top() == this_token) { + // ջջ token ָһλ + token_cnt++; + stack.pop(); + + if (this_token == "$") { + // ɹ + LL1_grammar_log.back() += "\taccept"; + } + else + { + // + LL1_grammar_log.back() += "\tmove"; + } + } + // Ϊս + else if (find(VTs.begin(), VTs.end(), stack.top()) != VTs.end()) { + if (stack.top() == "$") { + stack.pop(); + LL1_grammar_log.pop_back(); + } + else { + LL1_grammar_log.back() += "\terror"; + return; + } + } + else + { + auto tab = LL1_predict[stack.top()]; + + if (tab.find(this_token) == tab.end()) { + LL1_grammar_log.back() += "\terror"; + return; + } + else + { + auto this_rule = grammar_rules[tab[this_token]]; + stack.pop(); + for (int i = this_rule.second.size() - 1; i >= 0; i--) { + stack.push(this_rule.second[i]); + } + LL1_grammar_log.back() += "\treduction"; + } + } + } + +} + +void LL1::print_LL1_grammar_log() +{ + for (int i = 0; i < LL1_grammar_log.size(); ++i) { + cout << LL1_grammar_log[i] << endl; + } +} + +void LL1::fileout_LL1_grammar_log(string file_name) +{ + //򿪽ļ + ofstream outfile(file_name); + + if (!outfile.is_open()) { + cout << "ļʧ" << endl; + } + + for (int i = 0; i < LL1_grammar_log.size(); ++i) { + outfile << LL1_grammar_log[i] << endl; + } + outfile.close(); +} + +int LL1::insert_rule(pair>& new_rule) +{ + int cnt; + for (cnt = 0; cnt < grammar_rules.size(); cnt++) { + // ʽ дʽʱ + if (grammar_rules[cnt].first == new_rule.first && grammar_rules[cnt].second == new_rule.second) { + return cnt; + } + } + // ŵͬʱ + grammar_rules.push_back(new_rule); + return cnt; +} + diff --git a/LL1.h b/LL1.h new file mode 100644 index 0000000..f6f4960 --- /dev/null +++ b/LL1.h @@ -0,0 +1,32 @@ +// LL1 ﷨ +#ifndef LL1_H +#define LL1_H + +#include "grammar.h" + +using namespace std; + +class LL1:public Grammar{ +public: + LL1(); + ~LL1(); + + bool IsLL1(); // жϸķǷΪ LL1 ķ + void build_LL1_predict(); // LL1 Ԥ + void print_LL1_predict(); // ӡ LL1 Ԥ + void build_LL1_grammar(); // Լ + void print_LL1_grammar_log(); + void fileout_LL1_grammar_log(string file_name); + + +private: + unordered_map> select; // ŵ SELECT + unordered_map> LL1_predict; // LL1 Ԥ + vector LL1_grammar_log; // Լ + + int insert_rule(pair>& new_rule); // µĹ +}; + + + +#endif // !LL1_H diff --git a/RCa19240 b/RCa19240 new file mode 100644 index 0000000000000000000000000000000000000000..58da8a1c5667ed7f835bc1292bae2ab63a5ecd1f GIT binary patch literal 2794 zcmdT_O-my|5Pf*?APS!L;Kea>&}EI9uU*%hCNapyi8v-G1Y|cR8deiAW(5^gSlGiN z_z(O?fAB)KG?j2tr6=2}O=P8kC)Imem<| zovW`Dr49VVDe5>@k`GspfA8(#Q`tAYQ_M|R@>SyhBkrhDvy3qIY3n;BN1RWTUhf@q z+*P7KV;F(y@lho690t~Dy~KTWdQ?Y+J{c%b)fq!$E*|pB;VCsW_3W+vN?4x`HdC7qv z`R6#FDbzID!5Bvn8vuMvGNq>2Q(^q?V}O_Unp^$H2G^rD8^YF~@(ivo?A+-8u=TI- z(P4n>@W%ADRd0gQl!KSriq>@c6(W&Q-9Yq%J7^ILP8XHzXxus(5_4)@dPwj|2LFhC|7oV`Big+Bd-bHdB)f_)${gO|bwXrw*^wyK&NT}FM-X_vZgn<&T49VZ?c-V<&9A93r z*sFHkO7Fk7pP>m))%NPcgI%}1?p@($w|lqU-xAPl?!TP69_ie6tAH_UzVf;{cmgoP BS + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {15f31207-1716-408b-8c54-24c5a38fad6f} + compilegrammar + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/compile-grammar.vcxproj.filters b/compile-grammar.vcxproj.filters new file mode 100644 index 0000000..216da65 --- /dev/null +++ b/compile-grammar.vcxproj.filters @@ -0,0 +1,48 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + + + 源文件 + + + 源文件 + + + 源文件 + + + 源文件 + + + 源文件 + + + 源文件 + + + + + + + + + 头文件 + + + 头文件 + + + 头文件 + + + \ No newline at end of file diff --git a/dfa.cpp b/dfa.cpp new file mode 100644 index 0000000..263d069 --- /dev/null +++ b/dfa.cpp @@ -0,0 +1,251 @@ +#include "nfa.h" + +class Partition { +public: + set states; + Partition(set states) : states(states) {} +}; +/* +С㷨裺 +ȰнڵΪNAϣǽ̬ͽ̬ +S = {N,A}ȻַȥÿַSе״̬л֣ÿֱSȻͷһֱ֡S󣬼û״̬ɷΪֹ +c can split ssָSеһ״̬ +1.sÿ״̬¼ÿ״ַ̬c֮󵽴״̬Բ˵IJܡ +2.ѵ״̬࣬ݣͬһ״̬ĺһͬһ״ָ̬Sе״̬ +3.յڶķַsָ +ע⣺Ǵsзָȥsdzַc״̬sе״̬߳Բcַ״̬ +*/ + +// split ڽ״̬ϣgroupתƺһϸ֡ +// group: Ҫϸֵ״̬ +// input: ǰǵַ +// partitions: 洢зļϣҪϸ֣ڸü· +void split(const set& group, InputCharType input, set& partitions) { + // ڴ洢ÿĿӦ·״̬ϵӳ + map> targetPartitionsMap; + + for (State* state : group) { + auto it = state->transitions.find(input); + if (it != state->transitions.end()) { + State* targetState = *(it->second.begin());//DFA״̬תƾΨһ + // ڵǰзвҰĿ״̬ķ + for (Partition* partition : partitions) { + if (partition->states.find(targetState) != partition->states.end()) { + // ӳнǰ״̬ӵӦĿ + targetPartitionsMap[partition].insert(state); + break; + } + } + } + } + // group״̬ݵĿPartisetֵͬset + // ĿӳǷҪһϸ֣input״̬תڲͬĿļڲֿ + for (auto& entry : targetPartitionsMap) { + Partition* targetPartition = entry.first; + //targetPartitiongroup״̬ϼ£ + set& newGroupStates = entry.second; + //ڵִ֣ڵtargetPartitionֿҲԽﲻͬԴ״ָ̬ҲԷָĿ״̬֮״̬תƽִ + if (newGroupStates.size() < targetPartition->states.size()) { + for (State* state : newGroupStates) { + targetPartition->states.erase(state); + } + Partition* newGroup = new Partition(newGroupStates); + partitions.insert(newGroup); + } + } +} + +DFA minimizeDFA(const DFA& dfa) { + set partitions; + + // зֹ״ֳ̬һ飬ֹ״̬ WordType + /* + * ͬ WordType ֹ״̬ʾDzͬĴʷԪ͡ + * Щ״̬ڴʷовͬ,ܱϲΪͬһ״̬ + */ + map> endStateGroups; //ʼ̬ + set nonEndStates; //ʼ̬ + for (State* state : dfa.states) { + if (state->isFinalState) { + endStateGroups[state->wordType].insert(state);//ʹwordType̬Ͻһ + } + else { + nonEndStates.insert(state); + } + } + //ʼָǶ{N,A}Aչ̬ӿ㷨ٶȣչԭϣ + for (auto& entry : endStateGroups) { + Partition* endStateGroup = new Partition(entry.second); + partitions.insert(endStateGroup); + } + Partition* nonEndStateGroup = new Partition(nonEndStates); + partitions.insert(nonEndStateGroup); + //зָٷָԻСָ + size_t oldSize;//ָʼС + do { + oldSize = partitions.size(); + for (InputCharType input = static_cast(0); input < EPSILON; input = static_cast(input + 1)) {//Ia,Ib + for (Partition* partition : set(partitions)) {//ִָÿһǷٷָ + if (partition->states.size() > 1) {//Ϊ1ļϲٷָ + split(partition->states, input, partitions);//ķָ + } + } + } + } while (partitions.size() != oldSize);//ϴСٱ仯ʱֹͣ + + // µС DFAӳdfa±״̬ + // DFAΪDFA(State* set setset set states) + set minimizedStates; + set minimizedEndStates; + State* minimizedStartState = nullptr; + map stateMap; + + for (Partition* partition : partitions) {//õÿ + State* newState = new State(minimizedStates.size());// + // 鵱ǰǷDFAĿʼ״̬ǣ״̬ΪСDFAĿʼ״̬ + if (partition->states.find(dfa.startState) != partition->states.end()) { + minimizedStartState = newState; + } + // ֵ״̬ϲΪգѡһ״̬ + if (!partition->states.empty()) { + State* representative = *(partition->states.begin());//Ϊǰֹ״ֵ̬˲ͬҴСΪ1ֹ״̬beginѾԴ + //ڷָ״̬ϵĹУѾȷһ״̬ͬ,Ҫô״ֹ̬״̬Ҫôֹ״ֻ̬Ҫһ״̬ȷ״̬ǷӦֹ״̬ + // ״ֹ̬״̬״̬Ϊֹ״̬Ӧĵ + if (representative->isFinalState) { + newState->setFinalState(true, representative->wordType); + minimizedEndStates.insert(newState); + } + } + // о״̬ӳ䵽ͬһ״̬ + for (State* state : partition->states) + { + stateMap[state] = newState; + } + // ״̬뵽СDFA״̬ + minimizedStates.insert(newState); + } + // DFAе״̬ + for (State* oldState : dfa.states) { + // ͨӳҵ״̬Ӧ״̬ + State* newState = stateMap[oldState]; + for (const auto& transition : oldState->transitions) { + InputCharType input = transition.first; + State* oldTargetState = *(transition.second.begin());//dfaÿ״ֻ̬һת״̬nfaĽṹԼϴС<=1 + State* newTargetState = stateMap[oldTargetState];// ȡ״̬Ŀ״̬ + newState->addTransition(input, newTargetState);// ͨӳҵµĿ״̬ + } + } + + // ɾԭʼ + for (Partition* partition : partitions) { + delete partition; + } + return DFA(minimizedStartState, minimizedEndStates, minimizedStates); +} +void removeUnreachableStates(DFA& dfa) { + set reachableStates; //ɴ״̬ + queue statesQueue; //״̬ + + //ʼ״̬ɴ״̬ϺͶ + reachableStates.insert(dfa.startState); + statesQueue.push(dfa.startState); + + // BFS DFAҳпɴ״̬ + while (!statesQueue.empty()) { + State* currentState = statesQueue.front(); + statesQueue.pop(); + for (const auto& transition : currentState->transitions) { + State* targetState = *(transition.second.begin());//dfaÿ״ֻ̬һת״̬nfaĽṹԼϴС<=1 + if (reachableStates.find(targetState) == reachableStates.end()) {//δ + reachableStates.insert(targetState); + statesQueue.push(targetState); + } + } + } + + // ɾвɴ״̬ + for (auto it = dfa.states.begin(); it != dfa.states.end();) { + State* state = *it; + if (reachableStates.find(state) == reachableStates.end()) {//ǰ״̬ɴɾ + it = dfa.states.erase(it); + delete state; + } + else { + ++it; + } + } +} +vector recognize(const DFA& dfa, const string& input, const string& output) { + + State* currentState = dfa.startState; + State* nextState = nullptr; + string buffer; + vector tokens; // ռʶ𵽵Token + //򿪽ļ + ofstream file(output); + if (!file.is_open()) { + + std::cout << "Error opening file!" << std::endl; + return tokens; + } + for (size_t i = 0; i < input.length(); ++i) { + char c = input[i]; + if (c == ' '||c=='\n'||c=='\r\n'||c==' ')// ǿո񡢻еȷָ + {continue; } + InputCharType inputCharType = getInputCharType(c); + auto it = currentState->transitions.find(inputCharType); + + if (it != currentState->transitions.end()) { + nextState = *(it->second.begin()); + buffer.push_back(c); + + if (nextState->isFinalState && i + 1 < input.length()) {// һ״ֹ̬״̬һʣַ + char nextChar = input[i + 1]; + InputCharType nextInputCharType = getInputCharType(nextChar); + auto nextIt = nextState->transitions.find(nextInputCharType);// һ״̬תǷжӦַ + + if (nextIt == nextState->transitions.end()) {// ûиƥת + // ʶ𵽵ĵʷźͶӦ + cout << buffer << "\t<" << getWordTypeName(nextState->wordType,buffer) << ">" << endl; + file << buffer << "\t<" << getWordTypeName(nextState->wordType, buffer) << ">" << endl; + tokens.push_back(getGrammarName(nextState->wordType, buffer)); + buffer.clear(); + currentState = dfa.startState; + } + else { + currentState = nextState;// µǰ״̬Ϊһ״̬ + } + } + else { + currentState = nextState;// µǰ״̬Ϊһ״̬ + } + } + else {// ûҵƥת + if (currentState->isFinalState) {// ǰ״ֹ̬״̬ + // ʶ𵽵ĵʷźͶӦ + cout << buffer << "\t<" << getWordTypeName(currentState->wordType,buffer) << ">" << endl; + file << buffer << "\t<" << getWordTypeName(currentState->wordType, buffer) << ">" << endl; + tokens.push_back(getGrammarName(currentState->wordType, buffer) ); + buffer.clear(); + } + else { + // ǰ״ֹ̬״̬ + // ޷ʶַϢ + cout << "޷ʶַ: " << c << endl; + file << "޷ʶַ: " << c << endl; + + buffer.clear(); + } + currentState = dfa.startState;// صʼ״̬ + //--i;// ´ǰַ,ɣӴ + } + } + // һַΪҵǰ״ֹ̬״̬,Ӧһifelse + if (!buffer.empty() && currentState->isFinalState) { + cout << buffer << "\t<" << getWordTypeName(currentState->wordType,buffer) << ">" << endl; + file << buffer << "\t<" << getWordTypeName(currentState->wordType, buffer) << ">" << endl; + tokens.push_back(getGrammarName(currentState->wordType, buffer)); + } + file.close();//رļ + return tokens; +} \ No newline at end of file diff --git a/grammar.cpp b/grammar.cpp new file mode 100644 index 0000000..d83ea50 --- /dev/null +++ b/grammar.cpp @@ -0,0 +1,520 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "grammar.h" + + +Grammar::Grammar() +{ + +} + +Grammar::~Grammar() +{ + +} + + + +void Grammar::read_grammar() { + ifstream infile; + infile.open(grammar_file, ios::in); + if (!infile.is_open()) + { + cout << "ȡļʧ" << endl; + return; + } + + string buf; + string arrow = "->"; + string farrow; + bool start_flag = true; + string left; + string forms; + + while (!infile.eof()) { + // string + buf.clear(); + left.clear(); + forms.clear(); + farrow.clear(); + + + grammar_rules.push_back(pair>()); + getline(infile, buf); + + stringstream ss(buf); + + // ȡʽ + ss >> left; + grammar_rules.back().first = left; + symbols.push_back(left); + VNs.push_back(left); + + // 洢 start + if (start_flag) { + start = left; + start_flag = false; + } + + // ȡ -> ֤Ϸ + ss >> farrow; + if (farrow != arrow) { + cout << "﷨ȡ" << endl; + } + + // ȡʽҲ + while (ss >> forms) + { + grammar_rules.back().second.push_back(forms); + symbols.push_back(forms); + forms.clear(); + } + } + + // ż ս ȥ + set ssymbols(symbols.begin(), symbols.end()); + symbols.clear(); + symbols.resize(ssymbols.size()); + symbols.assign(ssymbols.begin(), ssymbols.end()); + + set sVNs(VNs.begin(), VNs.end()); + VNs.clear(); + VNs.resize(sVNs.size()); + VNs.assign(sVNs.begin(), sVNs.end()); + + // ż ս Ա֤ijɹ + sort(symbols.begin(), symbols.end()); + sort(VNs.begin(), VNs.end()); + + // ȡ õռ + set_difference(symbols.begin(), symbols.end(), VNs.begin(), VNs.end(), back_inserter(VTs)); + + infile.close(); + +} + +void Grammar::print_grammar() +{ + cout << "[start]: " << endl << start << endl << endl; + + cout << "[VTs]:" << endl; + for (int i = 0; i < VTs.size(); i++) { + cout << VTs[i] << " "; + if (((i + 1) % 5) == 0) + cout << endl; + } + cout << endl << endl; + + cout << "[VNs]:" << endl; + for (int i = 0; i < VNs.size(); i++) { + cout << VNs[i] << " "; + if (((i + 1) % 5) == 0) + cout << endl; + } + cout << endl << endl; + + cout << "[symbols]:" << endl; + for (int i = 0; i < symbols.size(); i++) { + cout << symbols[i] << " "; + if (((i + 1) % 5) == 0) + cout << endl; + } + cout << endl << endl; + + cout << "[grammar_rules]: " << grammar_rules.size() << endl; + for (int i = 0; i < grammar_rules.size(); ++i) { + cout << grammar_rules[i].first << " -> "; + for (int j = 0; j < grammar_rules[i].second.size(); ++j) { + cout << "\"" << grammar_rules[i].second[j] << "\" "; + } + cout << endl; + } + cout << endl << endl; +} + +void Grammar::expand_grammar() +{ + string new_start = start + "\'"; + pair> new_rule = pair>(new_start, vector()); + new_rule.second.push_back(start); + + VNs.push_back(new_start); + symbols.push_back(new_start); + grammar_rules.insert(grammar_rules.begin(), new_rule); + start = new_start; + + // ż + sort(symbols.begin(), symbols.end()); + +} + +void Grammar::init_grammar_set() +{ + string symbol; + + + + // ԷżиŽƵ ǷԵ $ շ + for (int i = 0; i < symbols.size(); i++) { + symbol = symbols[i]; + this->symbol_infer_empty(symbol); + symbol.clear(); + } + + // ʼڲʽ + init_appears_depend(); + + // ԷżиŽƵ FIRST + for (int i = 0; i < symbols.size(); i++) { + symbol = symbols[i]; + this->symbol_infer_first(symbol); + symbol.clear(); + } + + // ԷżиŽƵ FOLLOW + + // Ŷ + deque queue; + + // αз ɳʼ FOLLOW + + // start FOLLOW + follow[start] = this->symbol_infer_follow(start); + follow[start].push_back("$"); + queue.push_back(start); + + // start FOLLOW + for (int i = 0; i < symbols.size(); i++) { + symbol = symbols[i]; + if (symbol == start) { + symbol.clear(); + continue; + } + follow[symbol] = this->symbol_infer_follow(symbol); + queue.push_back(symbol); + symbol.clear(); + } + + // Ŷ нһ + while (!queue.empty()) { + // ȡ Ŷ ͷ + symbol = queue.front(); + queue.pop_front(); + + // FOLLOW ı + vector new_symbol_follow = this->symbol_infer_follow(symbol); + if (follow[symbol].size() < new_symbol_follow.size()) { + // ÷ з Ŷ + vector dep = depend[symbol]; + for (int i = 0; i < dep.size(); i++) { + queue.push_back(dep[i]); + } + follow[symbol] = new_symbol_follow; + } + symbol.clear(); + } + + +} + +void Grammar::print_grammar_set() +{ + // ӡڲʽij + cout << "[left_appears]:" << endl; + for (int i = 0; i < symbols.size(); i++) { + cout << "LEFT( " << symbols[i] << " ) = {"; + for (int j = 0; j < left_appears[symbols[i]].size(); j++) { + cout << " " << left_appears[symbols[i]][j] << " "; + } + cout << "}" << endl; + } + cout << endl << endl; + + cout << "[right_appears]:" << endl; + for (int i = 0; i < symbols.size(); i++) { + cout << "RIGHT( " << symbols[i] << " ) = {"; + for (int j = 0; j < right_appears[symbols[i]].size(); j++) { + cout << " " << right_appears[symbols[i]][j] << " "; + } + cout << "}" << endl; + } + cout << endl << endl; + + // ӡ FOLLOW ϵ + cout << "[depend]:" << endl; + for (int i = 0; i < symbols.size(); i++) { + cout << "DEPEND( " << symbols[i] << " ) = {"; + for (int j = 0; j < depend[symbols[i]].size(); j++) { + cout << " " << depend[symbols[i]][j] << " "; + } + cout << "}" << endl; + } + cout << endl << endl; + + + // ӡǷƵ $ շ + cout << "[infer_empty]:" << endl; + for (int i = 0; i < symbols.size(); i++) { + cout << symbols[i]<<" -> " << infer_empty[symbols[i]] << endl; + } + cout << endl << endl; + + // ӡ FIRST + cout << "[FIRST]:" << endl; + for (int i = 0; i < symbols.size(); i++) { + cout << "FIRST( " << symbols[i] << " ) = {"; + for (int j = 0; j < first[symbols[i]].size(); j++) { + cout << " " << first[symbols[i]][j] << " "; + } + cout << "}" << endl; + } + cout << endl << endl; + + // ӡ FOLLOW + cout << "[FOLLOW]:" << endl; + for (int i = 0; i < symbols.size(); i++) { + cout << "FOLLOW( " << symbols[i] << " ) = {"; + for (int j = 0; j < follow[symbols[i]].size(); j++) { + cout << " " << follow[symbols[i]][j] << " "; + } + cout << "}" << endl; + } + cout << endl << endl; + +} + +void Grammar::get_token_strings(vector& my_token_strings) +{ + token_strings.resize(my_token_strings.size()); + token_strings.assign(my_token_strings.begin(), my_token_strings.end()); + +} + +void Grammar::print_token_strings() +{ + for (int i = 0; i < token_strings.size(); i++) { + cout << token_strings[i] << endl; + } +} + +void Grammar::init_appears_depend() +{ + for (int k = 0; k < symbols.size(); k++) { + left_appears[symbols[k]] = vector(); + right_appears[symbols[k]] = vector(); + depend[symbols[k]] = vector(); + for (int i = 0; i < grammar_rules.size(); i++) { + if (grammar_rules[i].first == symbols[k]) { + // ʽ left + left_appears[symbols[k]].push_back(i); + + // Ըòʽϵ + for (int m = 0; m < grammar_rules[i].second.size(); m++) { + int n; + + // жϸòʽҲǷƵ $ շ + for (n = m + 1; n < grammar_rules[i].second.size(); n++) { + if (!infer_empty[grammar_rules[i].second[n]]) { + break; + } + } + // Ƶ ջķʽμ + if (n == grammar_rules[i].second.size()) { + if (symbols[k] != grammar_rules[i].second[m]) { + depend[symbols[k]].push_back(grammar_rules[i].second[m]); + } + } + + } + } + for (int j = 0; j < grammar_rules[i].second.size(); j++) { + // ʽҲ left + if (grammar_rules[i].second[j] == symbols[k]) { + right_appears[symbols[k]].push_back(i); + break; + } + } + } + } + +} + +bool Grammar::symbol_infer_empty(const string& symbol) { + + // ѾƵ + if (infer_empty.find(symbol) != infer_empty.end()) { + return infer_empty[symbol]; + } + + // ΪսʱҽΪ $ Ƶ $ + if (find(VTs.begin(), VTs.end(), symbol) != VTs.end()) { + infer_empty[symbol] = (symbol == "$") ; + return infer_empty[symbol]; + } + + // ΪսʱͨʽƵ + for (int i = 0; i < grammar_rules.size(); i++) { + // ÷Ϊʽʱ + if (grammar_rules[i].first == symbol) { + int j; + vector rule_right = grammar_rules[i].second; + for (j = 0; j < rule_right.size(); j++) { + // ݹƵ ʽҲ޷Ƶ $ ʱ + if (!(this->symbol_infer_empty(rule_right[j]))) { + break; + } + } + + // ҽʽҲƵ $ ʱ + if (j == rule_right.size()) { + infer_empty[symbol] = true; + return infer_empty[symbol]; + } + } + } + + // ʽ޷Ƶ $ ʱ޷Ƶ + infer_empty[symbol] = false; + return infer_empty[symbol]; + +} + +vector Grammar::symbol_infer_first(const string& symbol) +{ + // ѾƵ FIRST + if (first.find(symbol) != first.end()) { + return first[symbol]; + } + + vector symbol_first; + + // Ϊսʱ FIRST Ϊ + if (find(VTs.begin(), VTs.end(), symbol) != VTs.end()) { + symbol_first.push_back(symbol); + first[symbol] = symbol_first; + return first[symbol]; + } + + // ΪսʱͨʽƵ + for (int i = 0; i < grammar_rules.size(); i++) { + // ÷Ϊʽʱ + if (grammar_rules[i].first == symbol) { + int j; + for (j = 0; j < grammar_rules[i].second.size(); j++) { + + // вʽҲ + vector firsts = symbol_infer_first(grammar_rules[i].second[j]); + for (int k = 0; k < firsts.size(); k++) { + symbol_first.push_back(firsts[k]); + } + + // ʽҲ޷Ƶ $ ַʱ ж + if (!infer_empty[grammar_rules[i].second[j]]) { + break; + } + + } + + // ҽʽҲƵ $ ʱ $ 뵽 FIRST + if (j == grammar_rules[i].second.size()) { + symbol_first.push_back("$"); + } + } + } + + // Եǰ FIRST ȥ + set ssymbol_first(symbol_first.begin(), symbol_first.end()); + symbol_first.clear(); + symbol_first.resize(ssymbol_first.size()); + symbol_first.assign(ssymbol_first.begin(), ssymbol_first.end()); + + sort(symbol_first.begin(), symbol_first.end()); + + // طս FIRST + first[symbol] = symbol_first; + return first[symbol]; +} + +vector Grammar::symbol_infer_follow(const string& symbol) +{ + vector symbol_follow; + + // ȡ÷ųЩʽҲ + vector right_appear = right_appears[symbol]; + for (int i = 0; i < right_appear.size(); i++) { + int cnt; + + // ȡòʽҲķ + vector rule_right = grammar_rules[right_appear[i]].second; + + // α òʽҲ ÷ һλ + for (cnt = 0; cnt < rule_right.size(); cnt++) { + if (rule_right[cnt] == symbol) { + break; + } + } + cnt++; + + // ʣʽҲ + for (; cnt < rule_right.size(); cnt++) { + + // λȡ Ԫ FIRST + vector symbol_first = first[rule_right[cnt]]; + + // FIRST ѭ symbol_follow + for (int j = 0; j < symbol_first.size(); j++) { + symbol_follow.push_back(symbol_first[j]); + } + + // ɴ $ жϱ + if (!infer_empty[rule_right[cnt]]) { + break; + } + } + + // ʣʽҲɵ $ ʱ + if (cnt == rule_right.size()) { + if (follow.find(grammar_rules[right_appear[i]].first) != follow.end()) { + + // ʽ FOLLOW 뵽 ǰŵ FOLLOW + vector first_follow = follow[grammar_rules[right_appear[i]].first]; + for (int j = 0; j < first_follow.size(); j++) { + symbol_follow.push_back(first_follow[j]); + } + + } + + } + + } + + // ɾҪ $ ַ + auto it = remove(symbol_follow.begin(), symbol_follow.end(), "$"); + auto it1 = symbol_follow.erase(it, symbol_follow.end()); + + + // Եǰ FOLLOW ȥ + set ssymbol_follow(symbol_follow.begin(), symbol_follow.end()); + symbol_follow.clear(); + symbol_follow.resize(ssymbol_follow.size()); + symbol_follow.assign(ssymbol_follow.begin(), ssymbol_follow.end()); + + sort(symbol_follow.begin(), symbol_follow.end()); + + + + return symbol_follow; +} + + + + + + + diff --git a/grammar.h b/grammar.h new file mode 100644 index 0000000..c8399da --- /dev/null +++ b/grammar.h @@ -0,0 +1,55 @@ +// ﷨ +#ifndef GRAMMAR_H +#define GRAMMAR_H + + +#include +#include +#include +#include +#include + +using namespace std; + +class Grammar +{ +public: + const char grammar_file[12] = "grammar.txt"; + + Grammar(); + ~Grammar(); + void read_grammar(); // ȡ﷨ + void print_grammar(); // ӡ﷨ + void expand_grammar(); // չ﷨ + void init_grammar_set(); // ʼ﷨ؼ + void print_grammar_set(); // ӡ﷨ؼ + void get_token_strings(vector &); // ȡ token_stirngs + void print_token_strings(); + +protected: + vector>> grammar_rules; // ʽ + string start; // ʼַ + vector symbols; // + vector VTs; // ս + vector VNs; // ս + unordered_map> first; // FIRST + unordered_map> follow; // FOLLOW + unordered_map infer_empty; // ǷƵ $ ַ + vector token_strings; + + +private: + unordered_map> left_appears; // ÷ųЩʽ + unordered_map> right_appears; // ÷ųЩʽҲ + unordered_map> depend; // FOLLOW ϵ + + + void init_appears_depend(); // ȡ appear depend + bool symbol_infer_empty(const string& symbol); // жϷǷƵ $ ַ + vector symbol_infer_first(const string& symbol);// Ƶŵ FIRST + vector symbol_infer_follow(const string& symbol);// Ƶŵ FOLLOW + +}; + + +#endif // !GRAMMAR_H \ No newline at end of file diff --git a/grammar.txt b/grammar.txt new file mode 100644 index 0000000..b376f1b --- /dev/null +++ b/grammar.txt @@ -0,0 +1,70 @@ +program -> compUnit +compUnit -> decl compUnit +compUnit -> funcDef compUnit +compUnit -> $ +decl -> constDecl +decl -> varDecl +constDecl -> const bType constDef argConst ; +argConst -> , constDef argConst +argConst -> $ +constDef -> IDN = constInitVal +constInitVal -> constExp +constExp -> assignExp +varDecl -> bType varDef argVarDecl ; +argVarDecl -> , varDef argVarDecl +argVarDecl -> $ +varDef -> IDN argVarDef +argVarDef -> = initVal +argVarDef -> $ +initVal -> exp +bType -> int +funcDef -> funcType IDN ( funcFParams ) block +funcType -> void +funcFParams -> funcFParam argFunctionF +funcFParams -> $ +argFunctionF -> , funcFParam argFunctionF +argFunctionF -> $ +funcFParam -> bType IDN +block -> { blockItem } +blockItem -> decl blockItem +blockItem -> stmt blockItem +blockItem -> $ +stmt -> exp ; +stmt -> ; +stmt -> block +stmt -> return argExp ; +callFunc -> ( funcRParams ) +callFunc -> $ +funcRParam -> exp +funcRParams -> funcRParam argFunctionR +funcRParams -> $ +argFunctionR -> , funcRParam argFunctionR +argFunctionR -> $ +argExp -> exp +argExp -> $ +exp -> assignExp +assignExp -> eqExp assignExpAtom +assignExpAtom -> = eqExp assignExpAtom +assignExpAtom -> $ +eqExp -> relExp eqExpAtom +eqExpAtom -> == relExp eqExpAtom +eqExpAtom -> != relExp eqExpAtom +eqExpAtom -> $ +relExp -> addExp relExpAtom +relExpAtom -> < addExp relExpAtom +relExpAtom -> > addExp relExpAtom +relExpAtom -> <= addExp relExpAtom +relExpAtom -> >= addExp relExpAtom +relExpAtom -> $ +addExp -> mulExp addExpAtom +addExpAtom -> + mulExp addExpAtom +addExpAtom -> - mulExp addExpAtom +addExpAtom -> $ +mulExp -> unaryExp mulExpAtom +mulExpAtom -> * unaryExp mulExpAtom +mulExpAtom -> / unaryExp mulExpAtom +mulExpAtom -> % unaryExp mulExpAtom +mulExpAtom -> $ +number -> INT +unaryExp -> number +unaryExp -> IDN callFunc \ No newline at end of file diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..d09efe9 --- /dev/null +++ b/main.cpp @@ -0,0 +1,79 @@ +#include +#include +#include + +#include "nfa.h" +#include "grammar.h" +#include "LL1.h" +using namespace std; + +int main(int argc, char** argv) { + cout << "hello world" << endl; + + + //NFA nfa = buildNFA("./nfa.txt"); + NFA nfa = RexToNFA(); + //printNFA(nfa); + DFA dfa = nfaToDFA(nfa); + //printDFA(dfa); + DFA minimizedDFA = minimizeDFA(minimizeDFA(dfa)); + removeUnreachableStates(minimizedDFA); + printDFA(minimizedDFA); + + string inputs[6] = { + "tests/00/00.txt", + "tests/01/01.txt", + "tests/02/02.txt", + "tests/07/07.txt", + "tests/08_ʾ/08.txt", + "tests/10_ʾ/10.txt" + }; + + string outputs_lexical[6] = { + "tests/00/00_my_lexical.txt", + "tests/01/01_my_lexical.txt", + "tests/02/02_my_lexical.txt", + "tests/07/07_my_lexical.txt", + "tests/08_ʾ/08_my_lexical.txt", + "tests/10_ʾ/10_my_lexical.txt" + }; + + string outputs_grammar[6] = { + "tests/00/00_my_grammar.txt", + "tests/01/01_my_grammar.txt", + "tests/02/02_my_grammar.txt", + "tests/07/07_my_grammar.txt", + "tests/08_ʾ/08_my_grammar.txt", + "tests/10_ʾ/10_my_grammar.txt" + }; + + int i = 0; + LL1 ll; + ll.print_grammar_set(); + for (auto input : inputs) { + string content = readfile(input); + vector token_strings = recognize(minimizedDFA, content, outputs_lexical[i]); + + LL1 ll; + bool flag = ll.IsLL1(); + ll.build_LL1_predict(); + + + // ll.print_LL1_predict(); + ll.get_token_strings(token_strings); + + + // ll.print_token_strings(); + ll.build_LL1_grammar(); + + + ll.fileout_LL1_grammar_log(outputs_grammar[i]); + // ll.print_LL1_grammar_log(); + cout << endl; + i++; + } + + + + return 0; +} \ No newline at end of file diff --git a/nfa.cpp b/nfa.cpp new file mode 100644 index 0000000..519943b --- /dev/null +++ b/nfa.cpp @@ -0,0 +1,253 @@ +#include "nfa.h" +NFA RexToNFA() { + //||,Բͬʹÿոָ| lletter_»ߣ0(ҲdΪʹѾеĺ) + //[lu]l|u + string rex = "+ - * / % = > < == <= >= != && || ( ) { } , ; [l_][l_0]* -?00*"; + //Ӧ(̬) + vector finalState = { + OP_ADD, OP_SUB,OP_MUL,OP_DIV,OP_MOD,OP_ASSIGN,OP_GT,OP_LT, OP_EQ,OP_LE,OP_GE,OP_NE, OP_AND, OP_OR,SE_LBRAC, SE_RBRAC, + SE_LCBRAC,SE_RCBRAC,SE_COMMA,SE_SEMI,IDN,INT_VAL + }; + stringstream ss(rex); + string target; + + // ʼ״̬ + int stateIndex = 0; + int finalIndex = 0; + State* startState = new State(stateIndex++); + set endStates; + set allStates = { startState }; + while (getline(ss, target,' ')) { + //[l_][l_0]* + State* currentState = startState; + + for (size_t i = 0; i < target.length();i++) { + //һ״̬startStateͨInputCharType״̬ + State* newState = new State(stateIndex++); + allStates.insert(newState); + //Ҫһ + if (target[i] == '[') { + //[...]һ룬鿴]ǷУ*жϵǰ״̬Ĺ + for (i=i+1; i < target.length() && target[i] != ']'; i++) { + InputCharType input = getInputCharType(target[i]); + if (input != EPSILON) { + // תƺӵǰ״̬״̬ת + currentState->addTransition(input, newState); + } + } + } + else { + InputCharType input = getInputCharType(target[i]); + currentState->addTransition(input, newState); + } + //鿴һ + if (i + 1 < target.length() && target[i + 1] == '?') { + //EPSILONת״̬ + State* epsState = new State(stateIndex++); + allStates.insert(epsState); + currentState->addTransition(EPSILON, epsState); + newState->addTransition(EPSILON, epsState); + currentState = epsState; + // '?'ַ + i++; + } + else if (i + 1 < target.length() && target[i + 1] == '*') { + State* epsState = new State(stateIndex++); + allStates.insert(epsState); + currentState->addTransition(EPSILON, epsState); + newState->addTransition(EPSILON, epsState); + epsState->addTransition(EPSILON, currentState); + currentState = epsState; + // '*'ַ + i++; + } + else { + currentState = newState; + } + //жǷֹ״̬ + if (i == (target.length() - 1)) { + // һַǰ״̬Ϊֹ״̬ + currentState->setFinalState(true, finalState[endStates.size()]); + endStates.insert(currentState); + } + }//for + } + // ַ϶ӦNFA + return NFA(startState, endStates, allStates); +} + + +NFA buildNFA(string filename) { + ifstream ifs(filename); + if (!ifs) { + cerr << "Cannot open file: " << filename << endl; + exit(EXIT_FAILURE); + } + + int stateNum, inputNum; + ifs >> stateNum >> inputNum; + + vector states(stateNum); + for (int i = 0; i < stateNum; i++) { + states[i] = new State(i); + } + + State* startState = states[0]; + set endStates; + for (int i = 0; i < stateNum; i++) { + for (int j = 0; j < inputNum; j++) { + string targetStateIDs; + ifs >> targetStateIDs; + if (targetStateIDs.compare("#") != 0) { + stringstream ss(targetStateIDs); + string targetStateIDStr; + while (getline(ss, targetStateIDStr, ',')) { + int targetStateID = stoi(targetStateIDStr); + states[i]->addTransition(static_cast(j), states[targetStateID]); + } + } + } + } + + int endStateNum; + ifs >> endStateNum; + for (int i = 0; i < endStateNum; i++) { + int endStateID, wordTypeID; + ifs >> endStateID >> wordTypeID; + states[endStateID]->setFinalState(true, static_cast(wordTypeID)); + endStates.insert(states[endStateID]); + } + + return NFA(startState, endStates, set(states.begin(), states.end())); +} + +void printNFA(const NFA& nfa) { + cout << "Start state: " << nfa.startState->id << endl; + cout << "End states: "<id << " " << getWordTypeName(state->wordType) << " " << (state->isFinalState == true) << endl; + } + cout << endl; + + cout << "States and transitions:" << endl; + for (auto state : nfa.states) { + cout << "State " << state->id << ":" << endl; + for (auto transition : state->transitions) { + cout << "\tInput " << getInputChartypeName(transition.first) << ": "; + for (auto targetState : transition.second) { + cout << targetState->id << " "; + } + cout << endl; + } + } +} + +set move(const set& states, InputCharType input) { + set targetStates; + for (State* state : states) { + auto it = state->transitions.find(input); + if (it != state->transitions.end()) { + for (State* targetState : it->second) { + if (targetStates.find(targetState) == targetStates.end()) { + targetStates.insert(targetState); + } + } + } + } + return targetStates; +} + + +set epsilonClosure(const set& states) { + set closure = states; + stack stateStack; + for (State* state : states) { + stateStack.push(state); + } + while (!stateStack.empty()) { + State* currentState = stateStack.top(); + stateStack.pop(); + auto it = currentState->transitions.find(EPSILON); + if (it != currentState->transitions.end()) { + for (State* nextState : it->second) { + if (closure.find(nextState) == closure.end()) {//ֹͬһ״̬νջsetԴȥ + closure.insert(nextState); + stateStack.push(nextState); + } + } + } + } + return closure; +} + +DFA nfaToDFA(const NFA& nfa) { + map, State*, SetComparator> dfaStatesMap; // ӳNFA״̬ϵDFA״̬ӳ + queue> nfaStatesQueue; // BFSļ϶ + set dfaStates; + set dfaEndStates; + + set nfaStartClosure = epsilonClosure({ nfa.startState }); + State* dfaStartState = new State(0); + dfaStatesMap[nfaStartClosure] = dfaStartState; + dfaStates.insert(dfaStartState); + nfaStatesQueue.push(nfaStartClosure); + + int nextStateId = 1; + //set nfaStartClosure + while (!nfaStatesQueue.empty()) { + set currentNFAStates = nfaStatesQueue.front(); + nfaStatesQueue.pop(); + State* currentDFAState = dfaStatesMap[currentNFAStates]; + + // Ƿֹ״̬УDFA״̬Ϊֹ״̬ + for (State* nfaState : currentNFAStates) { + if (nfaState->isFinalState) { + // cout << nfaState->id << "is FinalState" << endl; + currentDFAState->setFinalState(true, nfaState->wordType); + dfaEndStates.insert(currentDFAState); + break; + } + } + + // ַ + for (int i = 0; i < static_cast(EPSILON); i++) { + InputCharType inputCharType = static_cast(i); + set nextNFAStates = epsilonClosure(move(currentNFAStates, inputCharType)); + if (nextNFAStates.empty()) { + continue; + } + + // NFA״̬ϲӳУ򴴽µDFA״̬ + if (dfaStatesMap.find(nextNFAStates) == dfaStatesMap.end()) { + State* newDFAState = new State(nextStateId++); + dfaStatesMap[nextNFAStates] = newDFAState; + dfaStates.insert(newDFAState); + nfaStatesQueue.push(nextNFAStates); + } + currentDFAState->addTransition(inputCharType, dfaStatesMap[nextNFAStates]); + } + } + + return DFA(dfaStartState, dfaEndStates, dfaStates); +} + +void printDFA(const DFA& dfa) { + cout << "Start state: " << dfa.startState->id << endl; + cout << "End states: "<id << " " << getWordTypeName(state->wordType) << endl; + } + cout << endl; + cout << "States and transitions:" << endl; + for (auto state : dfa.states) { + cout << "State " << state->id << ":" << endl; + for (auto transition : state->transitions) { + cout << "\tInput " << getInputChartypeName(transition.first) << ": "; + for (auto targetState : transition.second) { + cout << targetState->id << " "; + } + cout << endl; + } + } +} + diff --git a/nfa.h b/nfa.h new file mode 100644 index 0000000..f2c2666 --- /dev/null +++ b/nfa.h @@ -0,0 +1,173 @@ +#pragma once +#ifndef __NFA__H__ +#define __NFA__H__ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +//ʷŵͣ<еĵʷ,WordType> +typedef enum WordType { + //ʶɱʶжDzDZ֣úжIDN + KW_INT = 0, // int + KW_VOID, // void + KW_RETURN, // return + KW_CONST, // const + + OP_ADD, // + + OP_SUB, // - + OP_MUL, // * + OP_DIV, // / + OP_MOD, // % + OP_ASSIGN, // = + OP_GT, // > + OP_LT, // < + OP_EQ, // == + OP_LE, // <= + OP_GE, // >= + OP_NE, // != + OP_AND, // && + OP_OR, // || + + SE_LBRAC, // ( left backet + SE_RBRAC, // ) right bracket + SE_LCBRAC, // { left curly bracket + SE_RCBRAC, // } right curly bracket + SE_COMMA, // , + SE_SEMI, // ; + + IDN, // [a-zA-Z][a-zA-Z_0-9]* + INT_VAL, // -*[0-9]+ + UNKOWN +}WordType; +string getWordTypeName(WordType type); +//ַ +typedef enum InputCharType { + LETTER = 0, // ĸ 0 + UNDERLINE, // _ 1 + DIGIT, // 2 ʶɹһʱΪ˱01ǰȽһжϣGCC01ʶ𲢵1 + //OP + ADD, // + 3 + SUB, // - 4 + MUL, // * 5 + DIV, // / 6 + MOD, // % 7 + EQ, // = 8 + GT, // > 9 + LT, // < 10 + NOT, // ! 11 + AND, // & 12 + OR, // | 13 + //SE + LBRACKET, // ( 14 + RBRACKET, // ) 15 + LCBRAC, // { 16 + RCBRAC, // } 17 + COMMA, // , 18 + SEMI, // ; 19 + + EPSILON, // ַ 20 +}InputCharType; +string getInputChartypeName(InputCharType type); +enum class TokenType { + KW = 0, + OP, + SE, + IDN, + INT, + UNKNOWN +}; +TokenType getTokenType(WordType wordType,string buffer); +typedef struct Token { + string value; + TokenType type; +} Token; + +//庯жַ +InputCharType getInputCharType(char c); +string getWordTypeName(WordType type,string buffer); + +//״̬ +class State { +public: + int id; // ״̬ + map> transitions; // תƺӳ¼ÿַͶӦĿ״̬ + bool isFinalState; // ǷΪ״̬ + WordType wordType; // ״̬ʱӦ÷صĴʷԪ + State(int id) : id(id), isFinalState(false), wordType(UNKOWN) {} + void addTransition(InputCharType input, State* targetState) { + transitions[input].insert(targetState); + } + void setFinalState(bool isFinal, WordType type) { + isFinalState = isFinal; + wordType = type; + } + bool operator<(const State& other) const { + return id < other.id; + } +}; +//Ϊsetڲ򣬶ṹStatePtrCompare +struct StatePtrCompare { + bool operator()(const State* lhs, const State* rhs) const { + return lhs->id < rhs->id; + } +}; + +//NFA +class NFA { +public: + State* startState; // ʼ״̬ + set endStates; // ֹ״̬ + set states; // ״̬ + NFA(State* startState, set endStates, set states) : + startState(startState), endStates(endStates), states(states) {} + // void printNFA(); +}; +NFA RexToNFA(); +void printNFA(const NFA& nfa); +NFA buildNFA(string filename); +NFA RexToNFA(); +set move(const set& states, InputCharType input); +set epsilonClosure(const set& states); + +class DFA { +public: + State* startState; // ʼ״̬ + set endStates; // ֹ״̬ + set states; // ״̬ + DFA(State* startState, set endStates, set states) : + startState(startState), endStates(endStates), states(states) {} +}; +void removeUnreachableStates(DFA& dfa); +void printDFA(const DFA& dfa); +DFA nfaToDFA(const NFA& nfa); +void printDFA(const DFA& dfa); +struct SetComparator { + bool operator()(const set& a, const set& b) const { + if (a.size() != b.size()) { + return a.size() < b.size(); + } + + vector vecA(a.begin(), a.end()); + vector vecB(b.begin(), b.end()); + + sort(vecA.begin(), vecA.end(), [](const State* a, const State* b) { return a->id < b->id; }); + sort(vecB.begin(), vecB.end(), [](const State* a, const State* b) { return a->id < b->id; }); + + return vecA < vecB; + } +}; +string getGrammarName(WordType type, string buffer); +DFA minimizeDFA(const DFA& dfa); +vector recognize(const DFA& dfa, const string& input, const string& output); +string readfile(const string& filename); +#endif \ No newline at end of file diff --git a/nfa.txt b/nfa.txt new file mode 100644 index 0000000..7f4ece8 --- /dev/null +++ b/nfa.txt @@ -0,0 +1,56 @@ +30 21 +1 1 2 5 3,4 6 7 8 9,10 12,13 15,16 18 22 20 24 25 26 27 28 29 0 +1 1 1 # # # # # # # # # # # # # # # # # # +# # 2 # # # # # # # # # # # # # # # # # # +# # 2 # # # # # # # # # # # # # # # # # # +# # # # # # # # # # # # # # # # # # # # # +# # # # # # # # # # # # # # # # # # # # # +# # # # # # # # # # # # # # # # # # # # # +# # # # # # # # # # # # # # # # # # # # # +# # # # # # # # # # # # # # # # # # # # # +# # # # # # # # # # # # # # # # # # # # # +# # # # # # # # 11 # # # # # # # # # # # # +# # # # # # # # # # # # # # # # # # # # # +# # # # # # # # # # # # # # # # # # # # # +# # # # # # # # 14 # # # # # # # # # # # # +# # # # # # # # # # # # # # # # # # # # # +# # # # # # # # # # # # # # # # # # # # # +# # # # # # # # 17 # # # # # # # # # # # # +# # # # # # # # # # # # # # # # # # # # # +# # # # # # # # 19 # # # # # # # # # # # # +# # # # # # # # # # # # # # # # # # # # # +# # # # # # # # # # # # # 21 # # # # # # # +# # # # # # # # # # # # # # # # # # # # # +# # # # # # # # # # # # 23 # # # # # # # # +# # # # # # # # # # # # # # # # # # # # # +# # # # # # # # # # # # # # # # # # # # # +# # # # # # # # # # # # # # # # # # # # # +# # # # # # # # # # # # # # # # # # # # # +# # # # # # # # # # # # # # # # # # # # # +# # # # # # # # # # # # # # # # # # # # # +# # # # # # # # # # # # # # # # # # # # # +22 +1 24 +2 25 +4 5 +5 4 +6 6 +7 7 +8 8 +9 9 +11 12 +12 10 +14 14 +15 11 +17 13 +19 15 +21 17 +23 16 +24 18 +25 19 +26 20 +27 21 +28 22 +29 23 + + diff --git a/tests/00/00.txt b/tests/00/00.txt new file mode 100644 index 0000000..e6c5317 --- /dev/null +++ b/tests/00/00.txt @@ -0,0 +1,3 @@ +void main(){ + return 3; +} \ No newline at end of file diff --git a/tests/00/00_grammar.txt b/tests/00/00_grammar.txt new file mode 100644 index 0000000..d6f3185 --- /dev/null +++ b/tests/00/00_grammar.txt @@ -0,0 +1,34 @@ +program#void reduction +compUnit#void reduction +funcDef#void reduction +funcType#void reduction +void#void move +IDN#IDN move +(#( move +funcFParams#) reduction +)#) move +block#{ reduction +{#{ move +blockItem#return reduction +stmt#return reduction +return#return move +argExp#INT reduction +exp#INT reduction +assignExp#INT reduction +eqExp#INT reduction +relExp#INT reduction +addExp#INT reduction +mulExp#INT reduction +unaryExp#INT reduction +number#INT reduction +INT#INT move +mulExpAtom#; reduction +addExpAtom#; reduction +relExpAtom#; reduction +eqExpAtom#; reduction +assignExpAtom#; reduction +;#; move +blockItem#} reduction +}#} move +compUnit#EOF reduction +EOF#EOF accept diff --git a/tests/00/00_lexical.txt b/tests/00/00_lexical.txt new file mode 100644 index 0000000..9031a72 --- /dev/null +++ b/tests/00/00_lexical.txt @@ -0,0 +1,9 @@ +void +main +( +) +{ +return +3 +; +} diff --git a/tests/00/00_main.ll b/tests/00/00_main.ll new file mode 100644 index 0000000..c5918c3 --- /dev/null +++ b/tests/00/00_main.ll @@ -0,0 +1,23 @@ +; ModuleID = 'sysy2022_compiler' +source_filename = "../input/../input/00_main.sy" + +declare i32 @getint() + +declare i32 @getch() + +declare i32 @getarray(i32*) + +declare void @putint(i32) + +declare void @putch(i32) + +declare void @putarray(i32, i32*) + +declare void @starttime() + +declare void @stoptime() + +define void @defn() { +defn_ENTRY: + ret i32 3 +} diff --git a/tests/00/00_my_grammar.txt b/tests/00/00_my_grammar.txt new file mode 100644 index 0000000..d6f3185 --- /dev/null +++ b/tests/00/00_my_grammar.txt @@ -0,0 +1,34 @@ +program#void reduction +compUnit#void reduction +funcDef#void reduction +funcType#void reduction +void#void move +IDN#IDN move +(#( move +funcFParams#) reduction +)#) move +block#{ reduction +{#{ move +blockItem#return reduction +stmt#return reduction +return#return move +argExp#INT reduction +exp#INT reduction +assignExp#INT reduction +eqExp#INT reduction +relExp#INT reduction +addExp#INT reduction +mulExp#INT reduction +unaryExp#INT reduction +number#INT reduction +INT#INT move +mulExpAtom#; reduction +addExpAtom#; reduction +relExpAtom#; reduction +eqExpAtom#; reduction +assignExpAtom#; reduction +;#; move +blockItem#} reduction +}#} move +compUnit#EOF reduction +EOF#EOF accept diff --git a/tests/00/00_my_lexical.txt b/tests/00/00_my_lexical.txt new file mode 100644 index 0000000..9031a72 --- /dev/null +++ b/tests/00/00_my_lexical.txt @@ -0,0 +1,9 @@ +void +main +( +) +{ +return +3 +; +} diff --git a/tests/00/00_my_lexical1.txt b/tests/00/00_my_lexical1.txt new file mode 100644 index 0000000..9031a72 --- /dev/null +++ b/tests/00/00_my_lexical1.txt @@ -0,0 +1,9 @@ +void +main +( +) +{ +return +3 +; +} diff --git a/tests/00/grammar_diff.html b/tests/00/grammar_diff.html new file mode 100644 index 0000000..3eac34b --- /dev/null +++ b/tests/00/grammar_diff.html @@ -0,0 +1,83 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
t1program#void    reductiont1program#void    reduction
2compUnit#void   reduction2compUnit#void   reduction
3funcDef#void    reduction3funcDef#void    reduction
4funcType#void   reduction4funcType#void   reduction
5void#void       move5void#void       move
6IDN#IDN move6IDN#IDN move
7(#(     move7(#(     move
8funcFParams#)   reduction8funcFParams#)   reduction
9)#)     move9)#)     move
10block#{ reduction10block#{ reduction
11{#{     move11{#{     move
12blockItem#return        reduction12blockItem#return        reduction
13stmt#return     reduction13stmt#return     reduction
14return#return   move14return#return   move
15argExp#INT      reduction15argExp#INT      reduction
16exp#INT reduction16exp#INT reduction
17assignExp#INT   reduction17assignExp#INT   reduction
18eqExp#INT       reduction18eqExp#INT       reduction
19relExp#INT      reduction19relExp#INT      reduction
20addExp#INT      reduction20addExp#INT      reduction
21mulExp#INT      reduction21mulExp#INT      reduction
22unaryExp#INT    reduction22unaryExp#INT    reduction
23number#INT      reduction23number#INT      reduction
24INT#INT move24INT#INT move
25mulExpAtom#;    reduction25mulExpAtom#;    reduction
26addExpAtom#;    reduction26addExpAtom#;    reduction
27relExpAtom#;    reduction27relExpAtom#;    reduction
28eqExpAtom#;     reduction28eqExpAtom#;     reduction
29assignExpAtom#; reduction29assignExpAtom#; reduction
30;#;     move30;#;     move
31blockItem#}     reduction31blockItem#}     reduction
32}#}     move32}#}     move
33compUnit#EOF    reduction33compUnit#EOF    reduction
34EOF#EOF accept34EOF#EOF accept
+ + + + +
Legends
+ + + + +
Colors
 Added 
Changed
Deleted
+ + + + +
Links
(f)irst change
(n)ext change
(t)op
+ + + \ No newline at end of file diff --git a/tests/00/lexical_diff.html b/tests/00/lexical_diff.html new file mode 100644 index 0000000..f272bbd --- /dev/null +++ b/tests/00/lexical_diff.html @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
t1void    <KW>t1void    <KW>
2main    <IDN>2main    <IDN>
3(       <SE>3(       <SE>
4)       <SE>4)       <SE>
5{       <SE>5{       <SE>
6return  <KW>6return  <KW>
73       <INT>73       <INT>
8;       <SE>8;       <SE>
9}       <SE>9}       <SE>
+ + + + +
Legends
+ + + + +
Colors
 Added 
Changed
Deleted
+ + + + +
Links
(f)irst change
(n)ext change
(t)op
+ + + \ No newline at end of file diff --git a/tests/01/01.txt b/tests/01/01.txt new file mode 100644 index 0000000..7cc1168 --- /dev/null +++ b/tests/01/01.txt @@ -0,0 +1,7 @@ +int a = 3; +int b = 5; + +void main(){ + int a = 5; + return a + b; +} \ No newline at end of file diff --git a/tests/01/01_grammar.txt b/tests/01/01_grammar.txt new file mode 100644 index 0000000..2d24232 --- /dev/null +++ b/tests/01/01_grammar.txt @@ -0,0 +1,119 @@ +program#int reduction +compUnit#int reduction +decl#int reduction +varDecl#int reduction +bType#int reduction +int#int move +varDef#IDN reduction +IDN#IDN move +argVarDef#= reduction +=#= move +initVal#INT reduction +exp#INT reduction +assignExp#INT reduction +eqExp#INT reduction +relExp#INT reduction +addExp#INT reduction +mulExp#INT reduction +unaryExp#INT reduction +number#INT reduction +INT#INT move +mulExpAtom#; reduction +addExpAtom#; reduction +relExpAtom#; reduction +eqExpAtom#; reduction +assignExpAtom#; reduction +argVarDecl#; reduction +;#; move +compUnit#int reduction +decl#int reduction +varDecl#int reduction +bType#int reduction +int#int move +varDef#IDN reduction +IDN#IDN move +argVarDef#= reduction +=#= move +initVal#INT reduction +exp#INT reduction +assignExp#INT reduction +eqExp#INT reduction +relExp#INT reduction +addExp#INT reduction +mulExp#INT reduction +unaryExp#INT reduction +number#INT reduction +INT#INT move +mulExpAtom#; reduction +addExpAtom#; reduction +relExpAtom#; reduction +eqExpAtom#; reduction +assignExpAtom#; reduction +argVarDecl#; reduction +;#; move +compUnit#void reduction +funcDef#void reduction +funcType#void reduction +void#void move +IDN#IDN move +(#( move +funcFParams#) reduction +)#) move +block#{ reduction +{#{ move +blockItem#int reduction +decl#int reduction +varDecl#int reduction +bType#int reduction +int#int move +varDef#IDN reduction +IDN#IDN move +argVarDef#= reduction +=#= move +initVal#INT reduction +exp#INT reduction +assignExp#INT reduction +eqExp#INT reduction +relExp#INT reduction +addExp#INT reduction +mulExp#INT reduction +unaryExp#INT reduction +number#INT reduction +INT#INT move +mulExpAtom#; reduction +addExpAtom#; reduction +relExpAtom#; reduction +eqExpAtom#; reduction +assignExpAtom#; reduction +argVarDecl#; reduction +;#; move +blockItem#return reduction +stmt#return reduction +return#return move +argExp#IDN reduction +exp#IDN reduction +assignExp#IDN reduction +eqExp#IDN reduction +relExp#IDN reduction +addExp#IDN reduction +mulExp#IDN reduction +unaryExp#IDN reduction +IDN#IDN move +callFunc#+ reduction +mulExpAtom#+ reduction +addExpAtom#+ reduction ++#+ move +mulExp#IDN reduction +unaryExp#IDN reduction +IDN#IDN move +callFunc#; reduction +mulExpAtom#; reduction +addExpAtom#; reduction +relExpAtom#; reduction +eqExpAtom#; reduction +assignExpAtom#; reduction +;#; move +blockItem#} reduction +}#} move +compUnit#EOF reduction +EOF#EOF accept diff --git a/tests/01/01_lexical.txt b/tests/01/01_lexical.txt new file mode 100644 index 0000000..c045b5a --- /dev/null +++ b/tests/01/01_lexical.txt @@ -0,0 +1,26 @@ +int +a += +3 +; +int +b += +5 +; +void +main +( +) +{ +int +a += +5 +; +return +a ++ +b +; +} diff --git a/tests/01/01_my_grammar.txt b/tests/01/01_my_grammar.txt new file mode 100644 index 0000000..2d24232 --- /dev/null +++ b/tests/01/01_my_grammar.txt @@ -0,0 +1,119 @@ +program#int reduction +compUnit#int reduction +decl#int reduction +varDecl#int reduction +bType#int reduction +int#int move +varDef#IDN reduction +IDN#IDN move +argVarDef#= reduction +=#= move +initVal#INT reduction +exp#INT reduction +assignExp#INT reduction +eqExp#INT reduction +relExp#INT reduction +addExp#INT reduction +mulExp#INT reduction +unaryExp#INT reduction +number#INT reduction +INT#INT move +mulExpAtom#; reduction +addExpAtom#; reduction +relExpAtom#; reduction +eqExpAtom#; reduction +assignExpAtom#; reduction +argVarDecl#; reduction +;#; move +compUnit#int reduction +decl#int reduction +varDecl#int reduction +bType#int reduction +int#int move +varDef#IDN reduction +IDN#IDN move +argVarDef#= reduction +=#= move +initVal#INT reduction +exp#INT reduction +assignExp#INT reduction +eqExp#INT reduction +relExp#INT reduction +addExp#INT reduction +mulExp#INT reduction +unaryExp#INT reduction +number#INT reduction +INT#INT move +mulExpAtom#; reduction +addExpAtom#; reduction +relExpAtom#; reduction +eqExpAtom#; reduction +assignExpAtom#; reduction +argVarDecl#; reduction +;#; move +compUnit#void reduction +funcDef#void reduction +funcType#void reduction +void#void move +IDN#IDN move +(#( move +funcFParams#) reduction +)#) move +block#{ reduction +{#{ move +blockItem#int reduction +decl#int reduction +varDecl#int reduction +bType#int reduction +int#int move +varDef#IDN reduction +IDN#IDN move +argVarDef#= reduction +=#= move +initVal#INT reduction +exp#INT reduction +assignExp#INT reduction +eqExp#INT reduction +relExp#INT reduction +addExp#INT reduction +mulExp#INT reduction +unaryExp#INT reduction +number#INT reduction +INT#INT move +mulExpAtom#; reduction +addExpAtom#; reduction +relExpAtom#; reduction +eqExpAtom#; reduction +assignExpAtom#; reduction +argVarDecl#; reduction +;#; move +blockItem#return reduction +stmt#return reduction +return#return move +argExp#IDN reduction +exp#IDN reduction +assignExp#IDN reduction +eqExp#IDN reduction +relExp#IDN reduction +addExp#IDN reduction +mulExp#IDN reduction +unaryExp#IDN reduction +IDN#IDN move +callFunc#+ reduction +mulExpAtom#+ reduction +addExpAtom#+ reduction ++#+ move +mulExp#IDN reduction +unaryExp#IDN reduction +IDN#IDN move +callFunc#; reduction +mulExpAtom#; reduction +addExpAtom#; reduction +relExpAtom#; reduction +eqExpAtom#; reduction +assignExpAtom#; reduction +;#; move +blockItem#} reduction +}#} move +compUnit#EOF reduction +EOF#EOF accept diff --git a/tests/01/01_my_lexical.txt b/tests/01/01_my_lexical.txt new file mode 100644 index 0000000..c045b5a --- /dev/null +++ b/tests/01/01_my_lexical.txt @@ -0,0 +1,26 @@ +int +a += +3 +; +int +b += +5 +; +void +main +( +) +{ +int +a += +5 +; +return +a ++ +b +; +} diff --git a/tests/01/01_var_defn2.ll b/tests/01/01_var_defn2.ll new file mode 100644 index 0000000..9ca840a --- /dev/null +++ b/tests/01/01_var_defn2.ll @@ -0,0 +1,30 @@ +; ModuleID = 'sysy2022_compiler' +source_filename = "../input/../input/01_var_defn2.sy" + +@a = global i32 3 +@b = global i32 5 +declare i32 @getint() + +declare i32 @getch() + +declare i32 @getarray(i32*) + +declare void @putint(i32) + +declare void @putch(i32) + +declare void @putarray(i32, i32*) + +declare void @starttime() + +declare void @stoptime() + +define void @defn() { +defn_ENTRY: + %op0 = alloca i32 + store i32 5, i32* %op0 + %op1 = load i32, i32* %op0 + %op2 = load i32, i32* @b + %op3 = add i32 %op1, %op2 + ret i32 %op3 +} diff --git a/tests/01/grammar_diff.html b/tests/01/grammar_diff.html new file mode 100644 index 0000000..37cdeba --- /dev/null +++ b/tests/01/grammar_diff.html @@ -0,0 +1,168 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
t1program#int     reductiont1program#int     reduction
2compUnit#int    reduction2compUnit#int    reduction
3decl#int        reduction3decl#int        reduction
4varDecl#int     reduction4varDecl#int     reduction
5bType#int       reduction5bType#int       reduction
6int#int move6int#int move
7varDef#IDN      reduction7varDef#IDN      reduction
8IDN#IDN move8IDN#IDN move
9argVarDef#=     reduction9argVarDef#=     reduction
10=#=     move10=#=     move
11initVal#INT     reduction11initVal#INT     reduction
12exp#INT reduction12exp#INT reduction
13assignExp#INT   reduction13assignExp#INT   reduction
14eqExp#INT       reduction14eqExp#INT       reduction
15relExp#INT      reduction15relExp#INT      reduction
16addExp#INT      reduction16addExp#INT      reduction
17mulExp#INT      reduction17mulExp#INT      reduction
18unaryExp#INT    reduction18unaryExp#INT    reduction
19number#INT      reduction19number#INT      reduction
20INT#INT move20INT#INT move
21mulExpAtom#;    reduction21mulExpAtom#;    reduction
22addExpAtom#;    reduction22addExpAtom#;    reduction
23relExpAtom#;    reduction23relExpAtom#;    reduction
24eqExpAtom#;     reduction24eqExpAtom#;     reduction
25assignExpAtom#; reduction25assignExpAtom#; reduction
26argVarDecl#;    reduction26argVarDecl#;    reduction
27;#;     move27;#;     move
28compUnit#int    reduction28compUnit#int    reduction
29decl#int        reduction29decl#int        reduction
30varDecl#int     reduction30varDecl#int     reduction
31bType#int       reduction31bType#int       reduction
32int#int move32int#int move
33varDef#IDN      reduction33varDef#IDN      reduction
34IDN#IDN move34IDN#IDN move
35argVarDef#=     reduction35argVarDef#=     reduction
36=#=     move36=#=     move
37initVal#INT     reduction37initVal#INT     reduction
38exp#INT reduction38exp#INT reduction
39assignExp#INT   reduction39assignExp#INT   reduction
40eqExp#INT       reduction40eqExp#INT       reduction
41relExp#INT      reduction41relExp#INT      reduction
42addExp#INT      reduction42addExp#INT      reduction
43mulExp#INT      reduction43mulExp#INT      reduction
44unaryExp#INT    reduction44unaryExp#INT    reduction
45number#INT      reduction45number#INT      reduction
46INT#INT move46INT#INT move
47mulExpAtom#;    reduction47mulExpAtom#;    reduction
48addExpAtom#;    reduction48addExpAtom#;    reduction
49relExpAtom#;    reduction49relExpAtom#;    reduction
50eqExpAtom#;     reduction50eqExpAtom#;     reduction
51assignExpAtom#; reduction51assignExpAtom#; reduction
52argVarDecl#;    reduction52argVarDecl#;    reduction
53;#;     move53;#;     move
54compUnit#void   reduction54compUnit#void   reduction
55funcDef#void    reduction55funcDef#void    reduction
56funcType#void   reduction56funcType#void   reduction
57void#void       move57void#void       move
58IDN#IDN move58IDN#IDN move
59(#(     move59(#(     move
60funcFParams#)   reduction60funcFParams#)   reduction
61)#)     move61)#)     move
62block#{ reduction62block#{ reduction
63{#{     move63{#{     move
64blockItem#int   reduction64blockItem#int   reduction
65decl#int        reduction65decl#int        reduction
66varDecl#int     reduction66varDecl#int     reduction
67bType#int       reduction67bType#int       reduction
68int#int move68int#int move
69varDef#IDN      reduction69varDef#IDN      reduction
70IDN#IDN move70IDN#IDN move
71argVarDef#=     reduction71argVarDef#=     reduction
72=#=     move72=#=     move
73initVal#INT     reduction73initVal#INT     reduction
74exp#INT reduction74exp#INT reduction
75assignExp#INT   reduction75assignExp#INT   reduction
76eqExp#INT       reduction76eqExp#INT       reduction
77relExp#INT      reduction77relExp#INT      reduction
78addExp#INT      reduction78addExp#INT      reduction
79mulExp#INT      reduction79mulExp#INT      reduction
80unaryExp#INT    reduction80unaryExp#INT    reduction
81number#INT      reduction81number#INT      reduction
82INT#INT move82INT#INT move
83mulExpAtom#;    reduction83mulExpAtom#;    reduction
84addExpAtom#;    reduction84addExpAtom#;    reduction
85relExpAtom#;    reduction85relExpAtom#;    reduction
86eqExpAtom#;     reduction86eqExpAtom#;     reduction
87assignExpAtom#; reduction87assignExpAtom#; reduction
88argVarDecl#;    reduction88argVarDecl#;    reduction
89;#;     move89;#;     move
90blockItem#return        reduction90blockItem#return        reduction
91stmt#return     reduction91stmt#return     reduction
92return#return   move92return#return   move
93argExp#IDN      reduction93argExp#IDN      reduction
94exp#IDN reduction94exp#IDN reduction
95assignExp#IDN   reduction95assignExp#IDN   reduction
96eqExp#IDN       reduction96eqExp#IDN       reduction
97relExp#IDN      reduction97relExp#IDN      reduction
98addExp#IDN      reduction98addExp#IDN      reduction
99mulExp#IDN      reduction99mulExp#IDN      reduction
100unaryExp#IDN    reduction100unaryExp#IDN    reduction
101IDN#IDN move101IDN#IDN move
102callFunc#+      reduction102callFunc#+      reduction
103mulExpAtom#+    reduction103mulExpAtom#+    reduction
104addExpAtom#+    reduction104addExpAtom#+    reduction
105+#+     move105+#+     move
106mulExp#IDN      reduction106mulExp#IDN      reduction
107unaryExp#IDN    reduction107unaryExp#IDN    reduction
108IDN#IDN move108IDN#IDN move
109callFunc#;      reduction109callFunc#;      reduction
110mulExpAtom#;    reduction110mulExpAtom#;    reduction
111addExpAtom#;    reduction111addExpAtom#;    reduction
112relExpAtom#;    reduction112relExpAtom#;    reduction
113eqExpAtom#;     reduction113eqExpAtom#;     reduction
114assignExpAtom#; reduction114assignExpAtom#; reduction
115;#;     move115;#;     move
116blockItem#}     reduction116blockItem#}     reduction
117}#}     move117}#}     move
118compUnit#EOF    reduction118compUnit#EOF    reduction
119EOF#EOF accept119EOF#EOF accept
+ + + + +
Legends
+ + + + +
Colors
 Added 
Changed
Deleted
+ + + + +
Links
(f)irst change
(n)ext change
(t)op
+ + + \ No newline at end of file diff --git a/tests/01/lexical_diff.html b/tests/01/lexical_diff.html new file mode 100644 index 0000000..f3ce468 --- /dev/null +++ b/tests/01/lexical_diff.html @@ -0,0 +1,75 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
t1int     <KW>t1int     <KW>
2a       <IDN>2a       <IDN>
3=       <OP>3=       <OP>
43       <INT>43       <INT>
5;       <SE>5;       <SE>
6int     <KW>6int     <KW>
7b       <IDN>7b       <IDN>
8=       <OP>8=       <OP>
95       <INT>95       <INT>
10;       <SE>10;       <SE>
11void    <KW>11void    <KW>
12main    <IDN>12main    <IDN>
13(       <SE>13(       <SE>
14)       <SE>14)       <SE>
15{       <SE>15{       <SE>
16int     <KW>16int     <KW>
17a       <IDN>17a       <IDN>
18=       <OP>18=       <OP>
195       <INT>195       <INT>
20;       <SE>20;       <SE>
21return  <KW>21return  <KW>
22a       <IDN>22a       <IDN>
23+       <OP>23+       <OP>
24b       <IDN>24b       <IDN>
25;       <SE>25;       <SE>
26}       <SE>26}       <SE>
+ + + + +
Legends
+ + + + +
Colors
 Added 
Changed
Deleted
+ + + + +
Links
(f)irst change
(n)ext change
(t)op
+ + + \ No newline at end of file diff --git a/tests/02/02.txt b/tests/02/02.txt new file mode 100644 index 0000000..618cf4e --- /dev/null +++ b/tests/02/02.txt @@ -0,0 +1,7 @@ +void main(){ + int a, b0, _c; + a = 1; + b0 = 2; + _c = 3; + return b0 + _c; +} \ No newline at end of file diff --git a/tests/02/02_grammar.txt b/tests/02/02_grammar.txt new file mode 100644 index 0000000..92f9cd9 --- /dev/null +++ b/tests/02/02_grammar.txt @@ -0,0 +1,151 @@ +program#void reduction +compUnit#void reduction +funcDef#void reduction +funcType#void reduction +void#void move +IDN#IDN move +(#( move +funcFParams#) reduction +)#) move +block#{ reduction +{#{ move +blockItem#int reduction +decl#int reduction +varDecl#int reduction +bType#int reduction +int#int move +varDef#IDN reduction +IDN#IDN move +argVarDef#, reduction +argVarDecl#, reduction +,#, move +varDef#IDN reduction +IDN#IDN move +argVarDef#, reduction +argVarDecl#, reduction +,#, move +varDef#IDN reduction +IDN#IDN move +argVarDef#; reduction +argVarDecl#; reduction +;#; move +blockItem#IDN reduction +stmt#IDN reduction +exp#IDN reduction +assignExp#IDN reduction +eqExp#IDN reduction +relExp#IDN reduction +addExp#IDN reduction +mulExp#IDN reduction +unaryExp#IDN reduction +IDN#IDN move +callFunc#= reduction +mulExpAtom#= reduction +addExpAtom#= reduction +relExpAtom#= reduction +eqExpAtom#= reduction +assignExpAtom#= reduction +=#= move +eqExp#INT reduction +relExp#INT reduction +addExp#INT reduction +mulExp#INT reduction +unaryExp#INT reduction +number#INT reduction +INT#INT move +mulExpAtom#; reduction +addExpAtom#; reduction +relExpAtom#; reduction +eqExpAtom#; reduction +assignExpAtom#; reduction +;#; move +blockItem#IDN reduction +stmt#IDN reduction +exp#IDN reduction +assignExp#IDN reduction +eqExp#IDN reduction +relExp#IDN reduction +addExp#IDN reduction +mulExp#IDN reduction +unaryExp#IDN reduction +IDN#IDN move +callFunc#= reduction +mulExpAtom#= reduction +addExpAtom#= reduction +relExpAtom#= reduction +eqExpAtom#= reduction +assignExpAtom#= reduction +=#= move +eqExp#INT reduction +relExp#INT reduction +addExp#INT reduction +mulExp#INT reduction +unaryExp#INT reduction +number#INT reduction +INT#INT move +mulExpAtom#; reduction +addExpAtom#; reduction +relExpAtom#; reduction +eqExpAtom#; reduction +assignExpAtom#; reduction +;#; move +blockItem#IDN reduction +stmt#IDN reduction +exp#IDN reduction +assignExp#IDN reduction +eqExp#IDN reduction +relExp#IDN reduction +addExp#IDN reduction +mulExp#IDN reduction +unaryExp#IDN reduction +IDN#IDN move +callFunc#= reduction +mulExpAtom#= reduction +addExpAtom#= reduction +relExpAtom#= reduction +eqExpAtom#= reduction +assignExpAtom#= reduction +=#= move +eqExp#INT reduction +relExp#INT reduction +addExp#INT reduction +mulExp#INT reduction +unaryExp#INT reduction +number#INT reduction +INT#INT move +mulExpAtom#; reduction +addExpAtom#; reduction +relExpAtom#; reduction +eqExpAtom#; reduction +assignExpAtom#; reduction +;#; move +blockItem#return reduction +stmt#return reduction +return#return move +argExp#IDN reduction +exp#IDN reduction +assignExp#IDN reduction +eqExp#IDN reduction +relExp#IDN reduction +addExp#IDN reduction +mulExp#IDN reduction +unaryExp#IDN reduction +IDN#IDN move +callFunc#+ reduction +mulExpAtom#+ reduction +addExpAtom#+ reduction ++#+ move +mulExp#IDN reduction +unaryExp#IDN reduction +IDN#IDN move +callFunc#; reduction +mulExpAtom#; reduction +addExpAtom#; reduction +relExpAtom#; reduction +eqExpAtom#; reduction +assignExpAtom#; reduction +;#; move +blockItem#} reduction +}#} move +compUnit#EOF reduction +EOF#EOF accept diff --git a/tests/02/02_lexical.txt b/tests/02/02_lexical.txt new file mode 100644 index 0000000..a223421 --- /dev/null +++ b/tests/02/02_lexical.txt @@ -0,0 +1,30 @@ +void +main +( +) +{ +int +a +, +b0 +, +_c +; +a += +1 +; +b0 += +2 +; +_c += +3 +; +return +b0 ++ +_c +; +} diff --git a/tests/02/02_my_grammar.txt b/tests/02/02_my_grammar.txt new file mode 100644 index 0000000..92f9cd9 --- /dev/null +++ b/tests/02/02_my_grammar.txt @@ -0,0 +1,151 @@ +program#void reduction +compUnit#void reduction +funcDef#void reduction +funcType#void reduction +void#void move +IDN#IDN move +(#( move +funcFParams#) reduction +)#) move +block#{ reduction +{#{ move +blockItem#int reduction +decl#int reduction +varDecl#int reduction +bType#int reduction +int#int move +varDef#IDN reduction +IDN#IDN move +argVarDef#, reduction +argVarDecl#, reduction +,#, move +varDef#IDN reduction +IDN#IDN move +argVarDef#, reduction +argVarDecl#, reduction +,#, move +varDef#IDN reduction +IDN#IDN move +argVarDef#; reduction +argVarDecl#; reduction +;#; move +blockItem#IDN reduction +stmt#IDN reduction +exp#IDN reduction +assignExp#IDN reduction +eqExp#IDN reduction +relExp#IDN reduction +addExp#IDN reduction +mulExp#IDN reduction +unaryExp#IDN reduction +IDN#IDN move +callFunc#= reduction +mulExpAtom#= reduction +addExpAtom#= reduction +relExpAtom#= reduction +eqExpAtom#= reduction +assignExpAtom#= reduction +=#= move +eqExp#INT reduction +relExp#INT reduction +addExp#INT reduction +mulExp#INT reduction +unaryExp#INT reduction +number#INT reduction +INT#INT move +mulExpAtom#; reduction +addExpAtom#; reduction +relExpAtom#; reduction +eqExpAtom#; reduction +assignExpAtom#; reduction +;#; move +blockItem#IDN reduction +stmt#IDN reduction +exp#IDN reduction +assignExp#IDN reduction +eqExp#IDN reduction +relExp#IDN reduction +addExp#IDN reduction +mulExp#IDN reduction +unaryExp#IDN reduction +IDN#IDN move +callFunc#= reduction +mulExpAtom#= reduction +addExpAtom#= reduction +relExpAtom#= reduction +eqExpAtom#= reduction +assignExpAtom#= reduction +=#= move +eqExp#INT reduction +relExp#INT reduction +addExp#INT reduction +mulExp#INT reduction +unaryExp#INT reduction +number#INT reduction +INT#INT move +mulExpAtom#; reduction +addExpAtom#; reduction +relExpAtom#; reduction +eqExpAtom#; reduction +assignExpAtom#; reduction +;#; move +blockItem#IDN reduction +stmt#IDN reduction +exp#IDN reduction +assignExp#IDN reduction +eqExp#IDN reduction +relExp#IDN reduction +addExp#IDN reduction +mulExp#IDN reduction +unaryExp#IDN reduction +IDN#IDN move +callFunc#= reduction +mulExpAtom#= reduction +addExpAtom#= reduction +relExpAtom#= reduction +eqExpAtom#= reduction +assignExpAtom#= reduction +=#= move +eqExp#INT reduction +relExp#INT reduction +addExp#INT reduction +mulExp#INT reduction +unaryExp#INT reduction +number#INT reduction +INT#INT move +mulExpAtom#; reduction +addExpAtom#; reduction +relExpAtom#; reduction +eqExpAtom#; reduction +assignExpAtom#; reduction +;#; move +blockItem#return reduction +stmt#return reduction +return#return move +argExp#IDN reduction +exp#IDN reduction +assignExp#IDN reduction +eqExp#IDN reduction +relExp#IDN reduction +addExp#IDN reduction +mulExp#IDN reduction +unaryExp#IDN reduction +IDN#IDN move +callFunc#+ reduction +mulExpAtom#+ reduction +addExpAtom#+ reduction ++#+ move +mulExp#IDN reduction +unaryExp#IDN reduction +IDN#IDN move +callFunc#; reduction +mulExpAtom#; reduction +addExpAtom#; reduction +relExpAtom#; reduction +eqExpAtom#; reduction +assignExpAtom#; reduction +;#; move +blockItem#} reduction +}#} move +compUnit#EOF reduction +EOF#EOF accept diff --git a/tests/02/02_my_lexical.txt b/tests/02/02_my_lexical.txt new file mode 100644 index 0000000..a223421 --- /dev/null +++ b/tests/02/02_my_lexical.txt @@ -0,0 +1,30 @@ +void +main +( +) +{ +int +a +, +b0 +, +_c +; +a += +1 +; +b0 += +2 +; +_c += +3 +; +return +b0 ++ +_c +; +} diff --git a/tests/02/02_var_defn3.ll b/tests/02/02_var_defn3.ll new file mode 100644 index 0000000..4dc5973 --- /dev/null +++ b/tests/02/02_var_defn3.ll @@ -0,0 +1,35 @@ +; ModuleID = 'sysy2022_compiler' +source_filename = "../input/../input/02_var_defn3.sy" + +declare i32 @getint() + +declare i32 @getch() + +declare i32 @getarray(i32*) + +declare void @putint(i32) + +declare void @putch(i32) + +declare void @putarray(i32, i32*) + +declare void @starttime() + +declare void @stoptime() + +define void @defn() { +defn_ENTRY: + %op0 = alloca i32 + %op1 = alloca i32 + %op2 = alloca i32 + %op3 = load i32, i32* %op0 + store i32 %op3, i32 1 + %op4 = load i32, i32* %op1 + store i32 %op4, i32 2 + %op5 = load i32, i32* %op2 + store i32 %op5, i32 3 + %op6 = load i32, i32* %op1 + %op7 = load i32, i32* %op2 + %op8 = add i32 %op6, %op7 + ret i32 %op8 +} diff --git a/tests/02/grammar_diff.html b/tests/02/grammar_diff.html new file mode 100644 index 0000000..d3bbde4 --- /dev/null +++ b/tests/02/grammar_diff.html @@ -0,0 +1,200 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
t1program#void    reductiont1program#void    reduction
2compUnit#void   reduction2compUnit#void   reduction
3funcDef#void    reduction3funcDef#void    reduction
4funcType#void   reduction4funcType#void   reduction
5void#void       move5void#void       move
6IDN#IDN move6IDN#IDN move
7(#(     move7(#(     move
8funcFParams#)   reduction8funcFParams#)   reduction
9)#)     move9)#)     move
10block#{ reduction10block#{ reduction
11{#{     move11{#{     move
12blockItem#int   reduction12blockItem#int   reduction
13decl#int        reduction13decl#int        reduction
14varDecl#int     reduction14varDecl#int     reduction
15bType#int       reduction15bType#int       reduction
16int#int move16int#int move
17varDef#IDN      reduction17varDef#IDN      reduction
18IDN#IDN move18IDN#IDN move
19argVarDef#,     reduction19argVarDef#,     reduction
20argVarDecl#,    reduction20argVarDecl#,    reduction
21,#,     move21,#,     move
22varDef#IDN      reduction22varDef#IDN      reduction
23IDN#IDN move23IDN#IDN move
24argVarDef#,     reduction24argVarDef#,     reduction
25argVarDecl#,    reduction25argVarDecl#,    reduction
26,#,     move26,#,     move
27varDef#IDN      reduction27varDef#IDN      reduction
28IDN#IDN move28IDN#IDN move
29argVarDef#;     reduction29argVarDef#;     reduction
30argVarDecl#;    reduction30argVarDecl#;    reduction
31;#;     move31;#;     move
32blockItem#IDN   reduction32blockItem#IDN   reduction
33stmt#IDN        reduction33stmt#IDN        reduction
34exp#IDN reduction34exp#IDN reduction
35assignExp#IDN   reduction35assignExp#IDN   reduction
36eqExp#IDN       reduction36eqExp#IDN       reduction
37relExp#IDN      reduction37relExp#IDN      reduction
38addExp#IDN      reduction38addExp#IDN      reduction
39mulExp#IDN      reduction39mulExp#IDN      reduction
40unaryExp#IDN    reduction40unaryExp#IDN    reduction
41IDN#IDN move41IDN#IDN move
42callFunc#=      reduction42callFunc#=      reduction
43mulExpAtom#=    reduction43mulExpAtom#=    reduction
44addExpAtom#=    reduction44addExpAtom#=    reduction
45relExpAtom#=    reduction45relExpAtom#=    reduction
46eqExpAtom#=     reduction46eqExpAtom#=     reduction
47assignExpAtom#= reduction47assignExpAtom#= reduction
48=#=     move48=#=     move
49eqExp#INT       reduction49eqExp#INT       reduction
50relExp#INT      reduction50relExp#INT      reduction
51addExp#INT      reduction51addExp#INT      reduction
52mulExp#INT      reduction52mulExp#INT      reduction
53unaryExp#INT    reduction53unaryExp#INT    reduction
54number#INT      reduction54number#INT      reduction
55INT#INT move55INT#INT move
56mulExpAtom#;    reduction56mulExpAtom#;    reduction
57addExpAtom#;    reduction57addExpAtom#;    reduction
58relExpAtom#;    reduction58relExpAtom#;    reduction
59eqExpAtom#;     reduction59eqExpAtom#;     reduction
60assignExpAtom#; reduction60assignExpAtom#; reduction
61;#;     move61;#;     move
62blockItem#IDN   reduction62blockItem#IDN   reduction
63stmt#IDN        reduction63stmt#IDN        reduction
64exp#IDN reduction64exp#IDN reduction
65assignExp#IDN   reduction65assignExp#IDN   reduction
66eqExp#IDN       reduction66eqExp#IDN       reduction
67relExp#IDN      reduction67relExp#IDN      reduction
68addExp#IDN      reduction68addExp#IDN      reduction
69mulExp#IDN      reduction69mulExp#IDN      reduction
70unaryExp#IDN    reduction70unaryExp#IDN    reduction
71IDN#IDN move71IDN#IDN move
72callFunc#=      reduction72callFunc#=      reduction
73mulExpAtom#=    reduction73mulExpAtom#=    reduction
74addExpAtom#=    reduction74addExpAtom#=    reduction
75relExpAtom#=    reduction75relExpAtom#=    reduction
76eqExpAtom#=     reduction76eqExpAtom#=     reduction
77assignExpAtom#= reduction77assignExpAtom#= reduction
78=#=     move78=#=     move
79eqExp#INT       reduction79eqExp#INT       reduction
80relExp#INT      reduction80relExp#INT      reduction
81addExp#INT      reduction81addExp#INT      reduction
82mulExp#INT      reduction82mulExp#INT      reduction
83unaryExp#INT    reduction83unaryExp#INT    reduction
84number#INT      reduction84number#INT      reduction
85INT#INT move85INT#INT move
86mulExpAtom#;    reduction86mulExpAtom#;    reduction
87addExpAtom#;    reduction87addExpAtom#;    reduction
88relExpAtom#;    reduction88relExpAtom#;    reduction
89eqExpAtom#;     reduction89eqExpAtom#;     reduction
90assignExpAtom#; reduction90assignExpAtom#; reduction
91;#;     move91;#;     move
92blockItem#IDN   reduction92blockItem#IDN   reduction
93stmt#IDN        reduction93stmt#IDN        reduction
94exp#IDN reduction94exp#IDN reduction
95assignExp#IDN   reduction95assignExp#IDN   reduction
96eqExp#IDN       reduction96eqExp#IDN       reduction
97relExp#IDN      reduction97relExp#IDN      reduction
98addExp#IDN      reduction98addExp#IDN      reduction
99mulExp#IDN      reduction99mulExp#IDN      reduction
100unaryExp#IDN    reduction100unaryExp#IDN    reduction
101IDN#IDN move101IDN#IDN move
102callFunc#=      reduction102callFunc#=      reduction
103mulExpAtom#=    reduction103mulExpAtom#=    reduction
104addExpAtom#=    reduction104addExpAtom#=    reduction
105relExpAtom#=    reduction105relExpAtom#=    reduction
106eqExpAtom#=     reduction106eqExpAtom#=     reduction
107assignExpAtom#= reduction107assignExpAtom#= reduction
108=#=     move108=#=     move
109eqExp#INT       reduction109eqExp#INT       reduction
110relExp#INT      reduction110relExp#INT      reduction
111addExp#INT      reduction111addExp#INT      reduction
112mulExp#INT      reduction112mulExp#INT      reduction
113unaryExp#INT    reduction113unaryExp#INT    reduction
114number#INT      reduction114number#INT      reduction
115INT#INT move115INT#INT move
116mulExpAtom#;    reduction116mulExpAtom#;    reduction
117addExpAtom#;    reduction117addExpAtom#;    reduction
118relExpAtom#;    reduction118relExpAtom#;    reduction
119eqExpAtom#;     reduction119eqExpAtom#;     reduction
120assignExpAtom#; reduction120assignExpAtom#; reduction
121;#;     move121;#;     move
122blockItem#return        reduction122blockItem#return        reduction
123stmt#return     reduction123stmt#return     reduction
124return#return   move124return#return   move
125argExp#IDN      reduction125argExp#IDN      reduction
126exp#IDN reduction126exp#IDN reduction
127assignExp#IDN   reduction127assignExp#IDN   reduction
128eqExp#IDN       reduction128eqExp#IDN       reduction
129relExp#IDN      reduction129relExp#IDN      reduction
130addExp#IDN      reduction130addExp#IDN      reduction
131mulExp#IDN      reduction131mulExp#IDN      reduction
132unaryExp#IDN    reduction132unaryExp#IDN    reduction
133IDN#IDN move133IDN#IDN move
134callFunc#+      reduction134callFunc#+      reduction
135mulExpAtom#+    reduction135mulExpAtom#+    reduction
136addExpAtom#+    reduction136addExpAtom#+    reduction
137+#+     move137+#+     move
138mulExp#IDN      reduction138mulExp#IDN      reduction
139unaryExp#IDN    reduction139unaryExp#IDN    reduction
140IDN#IDN move140IDN#IDN move
141callFunc#;      reduction141callFunc#;      reduction
142mulExpAtom#;    reduction142mulExpAtom#;    reduction
143addExpAtom#;    reduction143addExpAtom#;    reduction
144relExpAtom#;    reduction144relExpAtom#;    reduction
145eqExpAtom#;     reduction145eqExpAtom#;     reduction
146assignExpAtom#; reduction146assignExpAtom#; reduction
147;#;     move147;#;     move
148blockItem#}     reduction148blockItem#}     reduction
149}#}     move149}#}     move
150compUnit#EOF    reduction150compUnit#EOF    reduction
151EOF#EOF accept151EOF#EOF accept
+ + + + +
Legends
+ + + + +
Colors
 Added 
Changed
Deleted
+ + + + +
Links
(f)irst change
(n)ext change
(t)op
+ + + \ No newline at end of file diff --git a/tests/02/lexical_diff.html b/tests/02/lexical_diff.html new file mode 100644 index 0000000..eadcef2 --- /dev/null +++ b/tests/02/lexical_diff.html @@ -0,0 +1,79 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
t1void    <KW>t1void    <KW>
2main    <IDN>2main    <IDN>
3(       <SE>3(       <SE>
4)       <SE>4)       <SE>
5{       <SE>5{       <SE>
6int     <KW>6int     <KW>
7a       <IDN>7a       <IDN>
8,       <SE>8,       <SE>
9b0      <IDN>9b0      <IDN>
10,       <SE>10,       <SE>
11_c      <IDN>11_c      <IDN>
12;       <SE>12;       <SE>
13a       <IDN>13a       <IDN>
14=       <OP>14=       <OP>
151       <INT>151       <INT>
16;       <SE>16;       <SE>
17b0      <IDN>17b0      <IDN>
18=       <OP>18=       <OP>
192       <INT>192       <INT>
20;       <SE>20;       <SE>
21_c      <IDN>21_c      <IDN>
22=       <OP>22=       <OP>
233       <INT>233       <INT>
24;       <SE>24;       <SE>
25return  <KW>25return  <KW>
26b0      <IDN>26b0      <IDN>
27+       <OP>27+       <OP>
28_c      <IDN>28_c      <IDN>
29;       <SE>29;       <SE>
30}       <SE>30}       <SE>
+ + + + +
Legends
+ + + + +
Colors
 Added 
Changed
Deleted
+ + + + +
Links
(f)irst change
(n)ext change
(t)op
+ + + \ No newline at end of file diff --git a/tests/07/07.txt b/tests/07/07.txt new file mode 100644 index 0000000..f490d8f --- /dev/null +++ b/tests/07/07.txt @@ -0,0 +1,4 @@ +void main(){ + const int a = 10, b = 5; + return b; +} \ No newline at end of file diff --git a/tests/07/07_const_var_defn3.ll b/tests/07/07_const_var_defn3.ll new file mode 100644 index 0000000..d3b95c2 --- /dev/null +++ b/tests/07/07_const_var_defn3.ll @@ -0,0 +1,23 @@ +; ModuleID = 'sysy2022_compiler' +source_filename = "../input/../input/07_const_var_defn3.sy" + +declare i32 @getint() + +declare i32 @getch() + +declare i32 @getarray(i32*) + +declare void @putint(i32) + +declare void @putch(i32) + +declare void @putarray(i32, i32*) + +declare void @starttime() + +declare void @stoptime() + +define void @defn() { +defn_ENTRY: + ret i32 5 +} diff --git a/tests/07/07_grammar.txt b/tests/07/07_grammar.txt new file mode 100644 index 0000000..db23ccd --- /dev/null +++ b/tests/07/07_grammar.txt @@ -0,0 +1,80 @@ +program#void reduction +compUnit#void reduction +funcDef#void reduction +funcType#void reduction +void#void move +IDN#IDN move +(#( move +funcFParams#) reduction +)#) move +block#{ reduction +{#{ move +blockItem#const reduction +decl#const reduction +constDecl#const reduction +const#const move +bType#int reduction +int#int move +constDef#IDN reduction +IDN#IDN move +=#= move +constInitVal#INT reduction +constExp#INT reduction +assignExp#INT reduction +eqExp#INT reduction +relExp#INT reduction +addExp#INT reduction +mulExp#INT reduction +unaryExp#INT reduction +number#INT reduction +INT#INT move +mulExpAtom#, reduction +addExpAtom#, reduction +relExpAtom#, reduction +eqExpAtom#, reduction +assignExpAtom#, reduction +argConst#, reduction +,#, move +constDef#IDN reduction +IDN#IDN move +=#= move +constInitVal#INT reduction +constExp#INT reduction +assignExp#INT reduction +eqExp#INT reduction +relExp#INT reduction +addExp#INT reduction +mulExp#INT reduction +unaryExp#INT reduction +number#INT reduction +INT#INT move +mulExpAtom#; reduction +addExpAtom#; reduction +relExpAtom#; reduction +eqExpAtom#; reduction +assignExpAtom#; reduction +argConst#; reduction +;#; move +blockItem#return reduction +stmt#return reduction +return#return move +argExp#IDN reduction +exp#IDN reduction +assignExp#IDN reduction +eqExp#IDN reduction +relExp#IDN reduction +addExp#IDN reduction +mulExp#IDN reduction +unaryExp#IDN reduction +IDN#IDN move +callFunc#; reduction +mulExpAtom#; reduction +addExpAtom#; reduction +relExpAtom#; reduction +eqExpAtom#; reduction +assignExpAtom#; reduction +;#; move +blockItem#} reduction +}#} move +compUnit#EOF reduction +EOF#EOF accept diff --git a/tests/07/07_lexical.txt b/tests/07/07_lexical.txt new file mode 100644 index 0000000..2bf2fd3 --- /dev/null +++ b/tests/07/07_lexical.txt @@ -0,0 +1,19 @@ +void +main +( +) +{ +const +int +a += +10 +, +b += +5 +; +return +b +; +} diff --git a/tests/07/07_my_grammar.txt b/tests/07/07_my_grammar.txt new file mode 100644 index 0000000..db23ccd --- /dev/null +++ b/tests/07/07_my_grammar.txt @@ -0,0 +1,80 @@ +program#void reduction +compUnit#void reduction +funcDef#void reduction +funcType#void reduction +void#void move +IDN#IDN move +(#( move +funcFParams#) reduction +)#) move +block#{ reduction +{#{ move +blockItem#const reduction +decl#const reduction +constDecl#const reduction +const#const move +bType#int reduction +int#int move +constDef#IDN reduction +IDN#IDN move +=#= move +constInitVal#INT reduction +constExp#INT reduction +assignExp#INT reduction +eqExp#INT reduction +relExp#INT reduction +addExp#INT reduction +mulExp#INT reduction +unaryExp#INT reduction +number#INT reduction +INT#INT move +mulExpAtom#, reduction +addExpAtom#, reduction +relExpAtom#, reduction +eqExpAtom#, reduction +assignExpAtom#, reduction +argConst#, reduction +,#, move +constDef#IDN reduction +IDN#IDN move +=#= move +constInitVal#INT reduction +constExp#INT reduction +assignExp#INT reduction +eqExp#INT reduction +relExp#INT reduction +addExp#INT reduction +mulExp#INT reduction +unaryExp#INT reduction +number#INT reduction +INT#INT move +mulExpAtom#; reduction +addExpAtom#; reduction +relExpAtom#; reduction +eqExpAtom#; reduction +assignExpAtom#; reduction +argConst#; reduction +;#; move +blockItem#return reduction +stmt#return reduction +return#return move +argExp#IDN reduction +exp#IDN reduction +assignExp#IDN reduction +eqExp#IDN reduction +relExp#IDN reduction +addExp#IDN reduction +mulExp#IDN reduction +unaryExp#IDN reduction +IDN#IDN move +callFunc#; reduction +mulExpAtom#; reduction +addExpAtom#; reduction +relExpAtom#; reduction +eqExpAtom#; reduction +assignExpAtom#; reduction +;#; move +blockItem#} reduction +}#} move +compUnit#EOF reduction +EOF#EOF accept diff --git a/tests/07/07_my_lexical.txt b/tests/07/07_my_lexical.txt new file mode 100644 index 0000000..2bf2fd3 --- /dev/null +++ b/tests/07/07_my_lexical.txt @@ -0,0 +1,19 @@ +void +main +( +) +{ +const +int +a += +10 +, +b += +5 +; +return +b +; +} diff --git a/tests/07/grammar_diff.html b/tests/07/grammar_diff.html new file mode 100644 index 0000000..4dc5165 --- /dev/null +++ b/tests/07/grammar_diff.html @@ -0,0 +1,129 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
t1program#void    reductiont1program#void    reduction
2compUnit#void   reduction2compUnit#void   reduction
3funcDef#void    reduction3funcDef#void    reduction
4funcType#void   reduction4funcType#void   reduction
5void#void       move5void#void       move
6IDN#IDN move6IDN#IDN move
7(#(     move7(#(     move
8funcFParams#)   reduction8funcFParams#)   reduction
9)#)     move9)#)     move
10block#{ reduction10block#{ reduction
11{#{     move11{#{     move
12blockItem#const reduction12blockItem#const reduction
13decl#const      reduction13decl#const      reduction
14constDecl#const reduction14constDecl#const reduction
15const#const     move15const#const     move
16bType#int       reduction16bType#int       reduction
17int#int move17int#int move
18constDef#IDN    reduction18constDef#IDN    reduction
19IDN#IDN move19IDN#IDN move
20=#=     move20=#=     move
21constInitVal#INT        reduction21constInitVal#INT        reduction
22constExp#INT    reduction22constExp#INT    reduction
23assignExp#INT   reduction23assignExp#INT   reduction
24eqExp#INT       reduction24eqExp#INT       reduction
25relExp#INT      reduction25relExp#INT      reduction
26addExp#INT      reduction26addExp#INT      reduction
27mulExp#INT      reduction27mulExp#INT      reduction
28unaryExp#INT    reduction28unaryExp#INT    reduction
29number#INT      reduction29number#INT      reduction
30INT#INT move30INT#INT move
31mulExpAtom#,    reduction31mulExpAtom#,    reduction
32addExpAtom#,    reduction32addExpAtom#,    reduction
33relExpAtom#,    reduction33relExpAtom#,    reduction
34eqExpAtom#,     reduction34eqExpAtom#,     reduction
35assignExpAtom#, reduction35assignExpAtom#, reduction
36argConst#,      reduction36argConst#,      reduction
37,#,     move37,#,     move
38constDef#IDN    reduction38constDef#IDN    reduction
39IDN#IDN move39IDN#IDN move
40=#=     move40=#=     move
41constInitVal#INT        reduction41constInitVal#INT        reduction
42constExp#INT    reduction42constExp#INT    reduction
43assignExp#INT   reduction43assignExp#INT   reduction
44eqExp#INT       reduction44eqExp#INT       reduction
45relExp#INT      reduction45relExp#INT      reduction
46addExp#INT      reduction46addExp#INT      reduction
47mulExp#INT      reduction47mulExp#INT      reduction
48unaryExp#INT    reduction48unaryExp#INT    reduction
49number#INT      reduction49number#INT      reduction
50INT#INT move50INT#INT move
51mulExpAtom#;    reduction51mulExpAtom#;    reduction
52addExpAtom#;    reduction52addExpAtom#;    reduction
53relExpAtom#;    reduction53relExpAtom#;    reduction
54eqExpAtom#;     reduction54eqExpAtom#;     reduction
55assignExpAtom#; reduction55assignExpAtom#; reduction
56argConst#;      reduction56argConst#;      reduction
57;#;     move57;#;     move
58blockItem#return        reduction58blockItem#return        reduction
59stmt#return     reduction59stmt#return     reduction
60return#return   move60return#return   move
61argExp#IDN      reduction61argExp#IDN      reduction
62exp#IDN reduction62exp#IDN reduction
63assignExp#IDN   reduction63assignExp#IDN   reduction
64eqExp#IDN       reduction64eqExp#IDN       reduction
65relExp#IDN      reduction65relExp#IDN      reduction
66addExp#IDN      reduction66addExp#IDN      reduction
67mulExp#IDN      reduction67mulExp#IDN      reduction
68unaryExp#IDN    reduction68unaryExp#IDN    reduction
69IDN#IDN move69IDN#IDN move
70callFunc#;      reduction70callFunc#;      reduction
71mulExpAtom#;    reduction71mulExpAtom#;    reduction
72addExpAtom#;    reduction72addExpAtom#;    reduction
73relExpAtom#;    reduction73relExpAtom#;    reduction
74eqExpAtom#;     reduction74eqExpAtom#;     reduction
75assignExpAtom#; reduction75assignExpAtom#; reduction
76;#;     move76;#;     move
77blockItem#}     reduction77blockItem#}     reduction
78}#}     move78}#}     move
79compUnit#EOF    reduction79compUnit#EOF    reduction
80EOF#EOF accept80EOF#EOF accept
+ + + + +
Legends
+ + + + +
Colors
 Added 
Changed
Deleted
+ + + + +
Links
(f)irst change
(n)ext change
(t)op
+ + + \ No newline at end of file diff --git a/tests/07/lexical_diff.html b/tests/07/lexical_diff.html new file mode 100644 index 0000000..f21ca90 --- /dev/null +++ b/tests/07/lexical_diff.html @@ -0,0 +1,68 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
t1void    <KW>t1void    <KW>
2main    <IDN>2main    <IDN>
3(       <SE>3(       <SE>
4)       <SE>4)       <SE>
5{       <SE>5{       <SE>
6const   <KW>6const   <KW>
7int     <KW>7int     <KW>
8a       <IDN>8a       <IDN>
9=       <OP>9=       <OP>
1010      <INT>1010      <INT>
11,       <SE>11,       <SE>
12b       <IDN>12b       <IDN>
13=       <OP>13=       <OP>
145       <INT>145       <INT>
15;       <SE>15;       <SE>
16return  <KW>16return  <KW>
17b       <IDN>17b       <IDN>
18;       <SE>18;       <SE>
19}       <SE>19}       <SE>
+ + + + +
Legends
+ + + + +
Colors
 Added 
Changed
Deleted
+ + + + +
Links
(f)irst change
(n)ext change
(t)op
+ + + \ No newline at end of file diff --git a/tests/08_编译错误示例/08.txt b/tests/08_编译错误示例/08.txt new file mode 100644 index 0000000..5d0d031 --- /dev/null +++ b/tests/08_编译错误示例/08.txt @@ -0,0 +1,7 @@ +int a = 3,= 1,c = 3; + + +void func(){ + int a = 5; + return a + b; +} \ No newline at end of file diff --git a/tests/08_编译错误示例/08_grammar.txt b/tests/08_编译错误示例/08_grammar.txt new file mode 100644 index 0000000..1a023d6 --- /dev/null +++ b/tests/08_编译错误示例/08_grammar.txt @@ -0,0 +1,28 @@ +program#int reduction +compUnit#int reduction +decl#int reduction +varDecl#int reduction +bType#int reduction +int#int move +varDef#IDN reduction +IDN#IDN move +argVarDef#= reduction +=#= move +initVal#INT reduction +exp#INT reduction +assignExp#INT reduction +eqExp#INT reduction +relExp#INT reduction +addExp#INT reduction +mulExp#INT reduction +unaryExp#INT reduction +number#INT reduction +INT#INT move +mulExpAtom#, reduction +addExpAtom#, reduction +relExpAtom#, reduction +eqExpAtom#, reduction +assignExpAtom#, reduction +argVarDecl#, reduction +,#, move +varDef#INT error diff --git a/tests/08_编译错误示例/08_lexical.txt b/tests/08_编译错误示例/08_lexical.txt new file mode 100644 index 0000000..d3731d3 --- /dev/null +++ b/tests/08_编译错误示例/08_lexical.txt @@ -0,0 +1,26 @@ +int +a += +3 +, +1 +, += +3 +; +void +func +( +) +{ +int +a += +5 +; +return +a ++ +b +; +} diff --git a/tests/08_编译错误示例/08_my_grammar.txt b/tests/08_编译错误示例/08_my_grammar.txt new file mode 100644 index 0000000..c958a84 --- /dev/null +++ b/tests/08_编译错误示例/08_my_grammar.txt @@ -0,0 +1,28 @@ +program#int reduction +compUnit#int reduction +decl#int reduction +varDecl#int reduction +bType#int reduction +int#int move +varDef#IDN reduction +IDN#IDN move +argVarDef#= reduction +=#= move +initVal#INT reduction +exp#INT reduction +assignExp#INT reduction +eqExp#INT reduction +relExp#INT reduction +addExp#INT reduction +mulExp#INT reduction +unaryExp#INT reduction +number#INT reduction +INT#INT move +mulExpAtom#, reduction +addExpAtom#, reduction +relExpAtom#, reduction +eqExpAtom#, reduction +assignExpAtom#, reduction +argVarDecl#, reduction +,#, move +varDef#= error diff --git a/tests/08_编译错误示例/08_my_lexical.txt b/tests/08_编译错误示例/08_my_lexical.txt new file mode 100644 index 0000000..20c2efa --- /dev/null +++ b/tests/08_编译错误示例/08_my_lexical.txt @@ -0,0 +1,28 @@ +int +a += +3 +, += +1 +, +c += +3 +; +void +func +( +) +{ +int +a += +5 +; +return +a ++ +b +; +} diff --git a/tests/08_编译错误示例/grammar_diff.html b/tests/08_编译错误示例/grammar_diff.html new file mode 100644 index 0000000..b2ef2d0 --- /dev/null +++ b/tests/08_编译错误示例/grammar_diff.html @@ -0,0 +1,77 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
t1program#int     reductiont1program#int     reduction
2compUnit#int    reduction2compUnit#int    reduction
3decl#int        reduction3decl#int        reduction
4varDecl#int     reduction4varDecl#int     reduction
5bType#int       reduction5bType#int       reduction
6int#int move6int#int move
7varDef#IDN      reduction7varDef#IDN      reduction
8IDN#IDN move8IDN#IDN move
9argVarDef#=     reduction9argVarDef#=     reduction
10=#=     move10=#=     move
11initVal#INT     reduction11initVal#INT     reduction
12exp#INT reduction12exp#INT reduction
13assignExp#INT   reduction13assignExp#INT   reduction
14eqExp#INT       reduction14eqExp#INT       reduction
15relExp#INT      reduction15relExp#INT      reduction
16addExp#INT      reduction16addExp#INT      reduction
17mulExp#INT      reduction17mulExp#INT      reduction
18unaryExp#INT    reduction18unaryExp#INT    reduction
19number#INT      reduction19number#INT      reduction
20INT#INT move20INT#INT move
21mulExpAtom#,    reduction21mulExpAtom#,    reduction
22addExpAtom#,    reduction22addExpAtom#,    reduction
23relExpAtom#,    reduction23relExpAtom#,    reduction
24eqExpAtom#,     reduction24eqExpAtom#,     reduction
25assignExpAtom#, reduction25assignExpAtom#, reduction
26argVarDecl#,    reduction26argVarDecl#,    reduction
27,#,     move27,#,     move
28varDef#INT      error28varDef#=        error
+ + + + +
Legends
+ + + + +
Colors
 Added 
Changed
Deleted
+ + + + +
Links
(f)irst change
(n)ext change
(t)op
+ + + \ No newline at end of file diff --git a/tests/08_编译错误示例/lexical_diff.html b/tests/08_编译错误示例/lexical_diff.html new file mode 100644 index 0000000..ea5a83a --- /dev/null +++ b/tests/08_编译错误示例/lexical_diff.html @@ -0,0 +1,77 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
t1int     <KW>t1int     <KW>
2a       <IDN>2a       <IDN>
3=       <OP>3=       <OP>
43       <INT>43       <INT>
5,       <SE>5,       <SE>
6=       <OP>
61       <INT>71       <INT>
7,       <SE>8,       <SE>
9c       <IDN>
8=       <OP>10=       <OP>
93       <INT>113       <INT>
10;       <SE>12;       <SE>
11void    <KW>13void    <KW>
12func    <IDN>14func    <IDN>
13(       <SE>15(       <SE>
14)       <SE>16)       <SE>
15{       <SE>17{       <SE>
16int     <KW>18int     <KW>
17a       <IDN>19a       <IDN>
18=       <OP>20=       <OP>
195       <INT>215       <INT>
20;       <SE>22;       <SE>
21return  <KW>23return  <KW>
22a       <IDN>24a       <IDN>
23+       <OP>25+       <OP>
24b       <IDN>26b       <IDN>
25;       <SE>27;       <SE>
26}       <SE>28}       <SE>
+ + + + +
Legends
+ + + + +
Colors
 Added 
Changed
Deleted
+ + + + +
Links
(f)irst change
(n)ext change
(t)op
+ + + \ No newline at end of file diff --git a/tests/10_编译错误示例/10.txt b/tests/10_编译错误示例/10.txt new file mode 100644 index 0000000..f83d73b --- /dev/null +++ b/tests/10_编译错误示例/10.txt @@ -0,0 +1,6 @@ +int a = 10 +void main(){ + int b + b = 2 + return b - a +} \ No newline at end of file diff --git a/tests/10_编译错误示例/10_grammar.txt b/tests/10_编译错误示例/10_grammar.txt new file mode 100644 index 0000000..13bb9a5 --- /dev/null +++ b/tests/10_编译错误示例/10_grammar.txt @@ -0,0 +1,26 @@ +program#int reduction +compUnit#int reduction +decl#int reduction +varDecl#int reduction +bType#int reduction +int#int move +varDef#IDN reduction +IDN#IDN move +argVarDef#= reduction +=#= move +initVal#INT reduction +exp#INT reduction +assignExp#INT reduction +eqExp#INT reduction +relExp#INT reduction +addExp#INT reduction +mulExp#INT reduction +unaryExp#INT reduction +number#INT reduction +INT#INT move +mulExpAtom#void reduction +addExpAtom#void reduction +relExpAtom#void reduction +eqExpAtom#void reduction +assignExpAtom#void reduction +argVarDecl#void error diff --git a/tests/10_编译错误示例/10_lexical.txt b/tests/10_编译错误示例/10_lexical.txt new file mode 100644 index 0000000..1f5cfa2 --- /dev/null +++ b/tests/10_编译错误示例/10_lexical.txt @@ -0,0 +1,19 @@ +int +a += +10 +void +main +( +) +{ +int +b +b += +2 +return +b +- +a +} diff --git a/tests/10_编译错误示例/10_my_grammar.txt b/tests/10_编译错误示例/10_my_grammar.txt new file mode 100644 index 0000000..729d2c3 --- /dev/null +++ b/tests/10_编译错误示例/10_my_grammar.txt @@ -0,0 +1,21 @@ +program#int reduction +compUnit#int reduction +decl#int reduction +varDecl#int reduction +bType#int reduction +int#int move +varDef#IDN reduction +IDN#IDN move +argVarDef#= reduction +=#= move +initVal#INT reduction +exp#INT reduction +assignExp#INT reduction +eqExp#INT reduction +relExp#INT reduction +addExp#INT reduction +mulExp#INT reduction +unaryExp#INT reduction +number#INT reduction +INT#INT move +mulExpAtom#void error diff --git a/tests/10_编译错误示例/10_my_lexical.txt b/tests/10_编译错误示例/10_my_lexical.txt new file mode 100644 index 0000000..1f5cfa2 --- /dev/null +++ b/tests/10_编译错误示例/10_my_lexical.txt @@ -0,0 +1,19 @@ +int +a += +10 +void +main +( +) +{ +int +b +b += +2 +return +b +- +a +} diff --git a/tests/10_编译错误示例/grammar_diff.html b/tests/10_编译错误示例/grammar_diff.html new file mode 100644 index 0000000..a9f7750 --- /dev/null +++ b/tests/10_编译错误示例/grammar_diff.html @@ -0,0 +1,75 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
t1program#int     reductiont1program#int     reduction
2compUnit#int    reduction2compUnit#int    reduction
3decl#int        reduction3decl#int        reduction
4varDecl#int     reduction4varDecl#int     reduction
5bType#int       reduction5bType#int       reduction
6int#int move6int#int move
7varDef#IDN      reduction7varDef#IDN      reduction
8IDN#IDN move8IDN#IDN move
9argVarDef#=     reduction9argVarDef#=     reduction
10=#=     move10=#=     move
11initVal#INT     reduction11initVal#INT     reduction
12exp#INT reduction12exp#INT reduction
13assignExp#INT   reduction13assignExp#INT   reduction
14eqExp#INT       reduction14eqExp#INT       reduction
15relExp#INT      reduction15relExp#INT      reduction
16addExp#INT      reduction16addExp#INT      reduction
17mulExp#INT      reduction17mulExp#INT      reduction
18unaryExp#INT    reduction18unaryExp#INT    reduction
19number#INT      reduction19number#INT      reduction
20INT#INT move20INT#INT move
21mulExpAtom#void reduction21mulExpAtom#void error
22addExpAtom#void reduction
23relExpAtom#void reduction
24eqExpAtom#void  reduction
25assignExpAtom#void      reduction
26argVarDecl#void error
+ + + + +
Legends
+ + + + +
Colors
 Added 
Changed
Deleted
+ + + + +
Links
(f)irst change
(n)ext change
(t)op
+ + + \ No newline at end of file diff --git a/tests/10_编译错误示例/lexical_diff.html b/tests/10_编译错误示例/lexical_diff.html new file mode 100644 index 0000000..09198ba --- /dev/null +++ b/tests/10_编译错误示例/lexical_diff.html @@ -0,0 +1,68 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
t1int     <KW>t1int     <KW>
2a       <IDN>2a       <IDN>
3=       <OP>3=       <OP>
410      <INT>410      <INT>
5void    <KW>5void    <KW>
6main    <IDN>6main    <IDN>
7(       <SE>7(       <SE>
8)       <SE>8)       <SE>
9{       <SE>9{       <SE>
10int     <KW>10int     <KW>
11b       <IDN>11b       <IDN>
12b       <IDN>12b       <IDN>
13=       <OP>13=       <OP>
142       <INT>142       <INT>
15return  <KW>15return  <KW>
16b       <IDN>16b       <IDN>
17-       <OP>17-       <OP>
18a       <IDN>18a       <IDN>
19}       <SE>19}       <SE>
+ + + + +
Legends
+ + + + +
Colors
 Added 
Changed
Deleted
+ + + + +
Links
(f)irst change
(n)ext change
(t)op
+ + + \ No newline at end of file diff --git a/tool.cpp b/tool.cpp new file mode 100644 index 0000000..e4ed05b --- /dev/null +++ b/tool.cpp @@ -0,0 +1,287 @@ +#include "nfa.h" + + +InputCharType getInputCharType(char c) { + switch (c) { + case '_': return UNDERLINE; + case '+': return ADD; + case '-': return SUB; + case '*': return MUL; + case '/': return DIV; + case '%': return MOD; + case '=': return EQ; + case '>': return GT; + case '<': return LT; + case '!': return NOT; + case '&': return AND; + case '|': return OR; + case '(': return LBRACKET; + case ')': return RBRACKET; + case '{': return LCBRAC; + case '}': return RCBRAC; + case ',': return COMMA; + case ';': return SEMI; + default: + if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) { + return LETTER; + } + else if (c >= '0' && c <= '9') { + return DIGIT; + } + else { + return EPSILON; + } + } +} +string getInputChartypeName(InputCharType type) { + switch (type) + { + case LETTER: + return "LETTER"; + case UNDERLINE: + return "UNDERLINE"; + case DIGIT: + return "DIGIT"; + case ADD: + return "+"; + case SUB: + return "-"; + case MUL: + return "*"; + case DIV: + return "/"; + case MOD: + return "%"; + case EQ: + return "="; + case GT: + return ">"; + case LT: + return "<"; + case NOT: + return "!"; + case AND: + return "&"; + case OR: + return "|"; + case LBRACKET: + return "("; + case RBRACKET: + return ")"; + case LCBRAC: + return "{"; + case RCBRAC: + return "}"; + case COMMA: + return ","; + case SEMI: + return ";"; + case EPSILON: + return "EPSILON"; + default: + return "UNKOWN"; + } +} +string getWordTypeName(WordType type, string buffer) { + switch (type) { + case OP_ADD: + case OP_SUB: + case OP_MUL: + case OP_DIV: + case OP_MOD: + case OP_ASSIGN: + case OP_GT: + case OP_LT: + case OP_EQ: + case OP_LE: + case OP_GE: + case OP_NE: + case OP_AND: + case OP_OR: + return "OP"; + + case SE_LBRAC: + case SE_RBRAC: + case SE_LCBRAC: + case SE_RCBRAC: + case SE_COMMA: + case SE_SEMI: + return "SE"; + + case IDN: + if (!buffer.compare("int") || !buffer.compare("void") || !buffer.compare("const") || !buffer.compare("return")){ + return "KW"; + } + else { + return "IDN"; + } + + case INT_VAL: + return "INT"; + + default: + return "UNKNOWN"; + } +} + +string readfile(const string& filename) +{ + // ļȡļ + ifstream file(filename); + + string content((istreambuf_iterator(file)), + istreambuf_iterator()); + + // ȥз + //removeǽַеijַƶַĩβһַָλõָ롣 + //erase ɾַָڵַ޸ĺַ + //content.erase(remove(content.begin(), content.end(), '\n'), content.end()); + + return content; +} +TokenType getTokenType(WordType type,string buffer) { + switch (type) { + case OP_ADD: + case OP_SUB: + case OP_MUL: + case OP_DIV: + case OP_MOD: + case OP_ASSIGN: + case OP_GT: + case OP_LT: + case OP_EQ: + case OP_LE: + case OP_GE: + case OP_NE: + case OP_AND: + case OP_OR: + return TokenType::OP; + + case SE_LBRAC: + case SE_RBRAC: + case SE_LCBRAC: + case SE_RCBRAC: + case SE_COMMA: + case SE_SEMI: + return TokenType::SE; + + case IDN: + if (!buffer.compare("int") || !buffer.compare("void") || !buffer.compare("const") || !buffer.compare("return")) { + return TokenType::KW; + } + else { + return TokenType::IDN; + } + + case INT_VAL: + return TokenType::INT; + + default: + return TokenType::UNKNOWN; + } +} + +string getWordTypeName(WordType type) { + switch (type) { + case KW_INT: + return "KW_INT"; + case KW_VOID: + return "KW_VOID"; + case KW_RETURN: + return "KW_RETURN"; + case KW_CONST: + return "KW_CONST"; + case OP_ADD: + return "OP_ADD"; + case OP_SUB: + return "OP_SUB"; + case OP_MUL: + return "OP_MUL"; + case OP_DIV: + return "OP_DIV"; + case OP_MOD: + return "OP_MOD"; + case OP_ASSIGN: + return "OP_ASSIGN"; + case OP_GT: + return "OP_GT"; + case OP_LT: + return "OP_LT"; + case OP_EQ: + return "OP_EQ"; + case OP_LE: + return "OP_LE"; + case OP_GE: + return "OP_GE"; + case OP_NE: + return "OP_NE"; + case OP_AND: + return "OP_AND"; + case OP_OR: + return "OP_OR"; + case SE_LBRAC: + return "SE_LBRAC"; + case SE_RBRAC: + return "SE_RBRAC"; + case SE_LCBRAC: + return "SE_LCBRAC"; + case SE_RCBRAC: + return "SE_RCBRAC"; + case SE_COMMA: + return "SE_COMMA"; + case SE_SEMI: + return "SE_SEMI"; + case IDN: + return "IDN"; + case INT_VAL: + return "INT_VAL"; + default: + return "UNKNOWN"; + } +} + +string getGrammarName(WordType type, string buffer) { + switch (type) { + + case OP_ADD: return "+"; + case OP_SUB: return "-"; + case OP_MUL: return "*"; + case OP_DIV: return "/"; + case OP_MOD: return "%"; + case OP_ASSIGN: return "="; + case OP_GT: return ">"; + case OP_LT: return "<"; + case OP_EQ: return "=="; + case OP_LE: return "<="; + case OP_GE: return ">="; + case OP_NE: return "!="; + case OP_AND: return "&&"; + case OP_OR: return "||"; + + case SE_LBRAC: return "("; + case SE_RBRAC: return ")"; + case SE_LCBRAC: return "{"; + case SE_RCBRAC: return "}"; + case SE_COMMA: return ","; + case SE_SEMI: return ";"; + + case IDN: + if (!buffer.compare("int")) { + return "int"; + } + else if (!buffer.compare("void")) { + return "void"; + } + else if (!buffer.compare("return")) { + return "return"; + } + else if (!buffer.compare("const")) { + return "const"; + } + else { + return "IDN"; + } + case INT_VAL: return "INT"; + default: cerr << "Token Error: "<< type << endl; exit(-1); + } +} \ No newline at end of file