資源簡介
掃描器和中間代碼生成器的實現,大作業是論文
ps:軟工lhj的編譯方法,認真做都90+,最后總評97,想拿高分第二個實驗要找她答辯

代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
using?namespace?std;
//關鍵詞表
vector?keywords?=?{
?“program“?“begin“?“end“?“var“?“while“?“do“
?“repeat“??“until“?“for“?“to“??“if“????“then“
?“else“????“;“?????“:“???“(“???“)“?????““
?“:=“??????“+“?????“-“???“*“???“/“?????“>“
?“>=“??????“==“????“<“???“<=“
};
//代表轉換圖中邊的值的種類
enum?Type
{
other //其他字符
digit //數字“0123456789“
point //小數點“.“
exponent //指數符號“E“或“e“
letter //字母“abcdefghijklmnopqrstuvwxyz“
delimeter //界符“;:()+-*/=><“
p_or_n //正負號
};
const?string?delimeter_str?=?“;:()+-*/=><“;
//當前字符的類型
Type?type;
//hash函數
struct?pair_hash_func
{
template
std::size_t?operator()?(const?std::pair?&p)?const
{
auto?h1?=?std::hash{}(p.first);
auto?h2?=?std::hash{}(p.second);
return?h1?^?h2;
}
};
//狀態變換表表示sigma(intType)?=?int任何非法的變換都會到達狀態0
unordered_map?int?pair_hash_func>?aut?=
{
{{1digit}2}?{{1letter}8}?{{1delimeter}9}?{{1other}15}
{{2digit}2}?{{2point}3}?{{2exponent}5}?{{2p_or_n}11}?{{2other}11}
{{3digit}4}
{{4digit}4}?{{4exponent}5}?{{4p_or_n}11}{{4other}11}
{{5digit}7}?{{5p_or_n}6}
{{6digit}7}
{{7digit}7}?{{7p_or_n}11}?{{7other}11}
{{8digit}8}?{{8letter}8}?{{8other}12}
{{9delimeter}10}{{9other}14}
{{10other}13}
};
vector?identifiers; //標識符表
vector?constants; //常數表
//token
struct?Token
{
int?_code;
int?_value;
Token()?=?default;
Token(int?c?int?v)?:_code(c)?_value(v)?{}
~Token()?=?default;
};
vector?token_seq; //token序列
string?curr_token; //當前單詞
int?n?p?m?e?t; //尾數值,指數值,小數位數,指數符號,類型
double?constant; //常數值
string?buffer; //源程序緩沖區
int?i; //源程序緩沖區指針,當前字符為buffer[i]
void?act(int); //狀態為state時的操作
int?find(int?char); //state接受value后的下一個狀態
int?insert_constant(double); //插入常數constant到常數表,返回constant在常數表的位置
int?reserve(const?string?&); //返回界符在keywords中的位置
int?insert_identifier(string?&); //插入標識符identifier到符號表,返回identifier在符號表的位置
Type?transform(char);
int?main(int?argc?char?*argv[])?{
//參數不足
if?(argc?!=?2)?{
cout?<“Missing?enough?arguments?at?least?1:?“?< exit(1);
}
string?fileName?=?argv[1];
ifstream?in(fileName?ios::in);
if?(in.fail())?{
cout?<“fail?in?opening?“?< exit(2);
}
int?curr_state; //當前狀態
//從文件流in中讀取一行保存到字符串buffer中
while?(getline(in?buffer))?{
i?=?0;?//從buffer的第一個字符開始
do?{
while?(buffer[i]?==?‘?‘)?{?//過濾空格
i++;
}
//判定一個單詞首字符的類別
//字母
if?(buffer[i]?>=?‘a‘?&&?buffer[i]?<=?‘z‘)?{
type?=?letter;
}
//數字
else?if?(buffer[i]?>=?‘0‘?&&?buffer[i]?<=?‘9‘)?{
type?=?digit;
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2020-07-29?11:42??編譯方法實驗源代碼+實驗報告+大作業\
?????目錄???????????0??2020-10-20?21:45??編譯方法實驗源代碼+實驗報告+大作業\大作業\
?????文件??????287980??2020-10-20?21:42??編譯方法實驗源代碼+實驗報告+大作業\大作業\大作業.docx
?????目錄???????????0??2020-10-20?21:45??編譯方法實驗源代碼+實驗報告+大作業\實驗報告\
?????文件?????2604994??2020-10-20?21:43??編譯方法實驗源代碼+實驗報告+大作業\實驗報告\實驗報告.docx
?????目錄???????????0??2020-07-29?11:42??編譯方法實驗源代碼+實驗報告+大作業\實驗源代碼\
?????目錄???????????0??2020-07-29?11:42??編譯方法實驗源代碼+實驗報告+大作業\實驗源代碼\實驗1\
?????目錄???????????0??2020-07-29?11:42??編譯方法實驗源代碼+實驗報告+大作業\實驗源代碼\實驗1\源代碼\
?????文件??????????71??2020-04-18?23:02??編譯方法實驗源代碼+實驗報告+大作業\實驗源代碼\實驗1\源代碼\exa.txt
?????文件??????????35??2020-04-18?23:04??編譯方法實驗源代碼+實驗報告+大作業\實驗源代碼\實驗1\源代碼\exa2.txt
?????文件????????8756??2020-04-19?10:35??編譯方法實驗源代碼+實驗報告+大作業\實驗源代碼\實驗1\源代碼\experiment1.cpp
?????文件???????44544??2020-04-18?22:43??編譯方法實驗源代碼+實驗報告+大作業\實驗源代碼\實驗1\源代碼\experiment1.exe
?????目錄???????????0??2020-07-29?11:42??編譯方法實驗源代碼+實驗報告+大作業\實驗源代碼\實驗1\運行結果\
?????目錄???????????0??2020-07-29?11:42??編譯方法實驗源代碼+實驗報告+大作業\實驗源代碼\實驗1\運行結果\異常處理\
?????文件???????40713??2020-04-18?22:49??編譯方法實驗源代碼+實驗報告+大作業\實驗源代碼\實驗1\運行結果\異常處理\參數缺失.jpg
?????文件???????39419??2020-04-18?22:51??編譯方法實驗源代碼+實驗報告+大作業\實驗源代碼\實驗1\運行結果\異常處理\文件不存在.jpg
?????文件???????66112??2020-04-18?22:59??編譯方法實驗源代碼+實驗報告+大作業\實驗源代碼\實驗1\運行結果\異常處理\詞法錯誤.jpg
?????文件???????74372??2020-04-18?23:00??編譯方法實驗源代碼+實驗報告+大作業\實驗源代碼\實驗1\運行結果\異常處理\非法字符.jpg
?????目錄???????????0??2020-07-29?11:42??編譯方法實驗源代碼+實驗報告+大作業\實驗源代碼\實驗1\運行結果\正常結果\
?????文件???????66930??2020-04-18?23:09??編譯方法實驗源代碼+實驗報告+大作業\實驗源代碼\實驗1\運行結果\正常結果\合法輸出1.jpg
?????文件???????54020??2020-04-18?23:04??編譯方法實驗源代碼+實驗報告+大作業\實驗源代碼\實驗1\運行結果\正常結果\合法輸出2.jpg
?????目錄???????????0??2020-07-29?11:42??編譯方法實驗源代碼+實驗報告+大作業\實驗源代碼\實驗2\
?????文件?????????615??2020-04-17?00:12??編譯方法實驗源代碼+實驗報告+大作業\實驗源代碼\實驗2\README.md
?????文件?????1270269??2020-04-17?09:15??編譯方法實驗源代碼+實驗報告+大作業\實驗源代碼\實驗2\實驗概覽.png
?????目錄???????????0??2020-07-29?11:42??編譯方法實驗源代碼+實驗報告+大作業\實驗源代碼\實驗2\源代碼\
?????文件??????325632??2020-04-17?00:05??編譯方法實驗源代碼+實驗報告+大作業\實驗源代碼\實驗2\源代碼\experiment2.exe
?????文件??????????56??2020-04-16?23:05??編譯方法實驗源代碼+實驗報告+大作業\實驗源代碼\實驗2\源代碼\grammer.txt
?????文件??????????70??2020-04-16?10:38??編譯方法實驗源代碼+實驗報告+大作業\實驗源代碼\實驗2\源代碼\grammer2.txt
?????文件???????16372??2020-04-17?00:10??編譯方法實驗源代碼+實驗報告+大作業\實驗源代碼\實驗2\源代碼\main.cpp
?????文件?????????833??2020-04-17?00:10??編譯方法實驗源代碼+實驗報告+大作業\實驗源代碼\實驗2\源代碼\util.h
?????目錄???????????0??2020-07-29?11:42??編譯方法實驗源代碼+實驗報告+大作業\實驗源代碼\實驗2\運行結果\
............此處省略15個文件信息
評論
共有 條評論