{"id":881,"date":"2022-05-08T17:29:49","date_gmt":"2022-05-08T09:29:49","guid":{"rendered":"https:\/\/kylelv.com\/?p=881"},"modified":"2022-05-08T17:29:50","modified_gmt":"2022-05-08T09:29:50","slug":"%e5%8c%97%e5%a4%a7%e7%bc%96%e8%af%91%e5%ae%9e%e8%b7%b52022-lab-lv1","status":"publish","type":"post","link":"https:\/\/blog.kylelv.com\/?p=881","title":{"rendered":"\u5317\u5927\u7f16\u8bd1\u5b9e\u8df52022 lab Lv1"},"content":{"rendered":"\n<p>\u4f5c\u4e3a\u65b0\u542f\u7528\u7684lab\u5b9e\u8df5\u65b9\u6848\uff0c\u52a9\u6559\u5c0f\u54e5\u54e5\u5199\u4e86\u4e00\u4e2a\u975e\u5e38\u8be6\u7ec6\u7684\u4e0a\u624b\u6587\u6863<a href=\"https:\/\/pku-minic.github.io\/online-doc\/#\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/pku-minic.github.io\/online-doc\/#\/<\/a>\uff0c\u8fd9\u91cc\u5c31\u8bb0\u5f55\u4e00\u4e0b\u5177\u4f53\u5b9e\u8df5\u6d41\u7a0b\u548c\u8e29\u8fc7\u7684\u5751x<\/p>\n\n\n\n<p>\u9996\u5148\u4e0a\u624b\u7684Lv1\uff0c\u53ef\u4ee5\u8bf4\u52a9\u6559xgg\u5df2\u7ecf\u5199\u7684\u975e\u5e38\u8be6\u5c3d\u4e86\uff0c\u57fa\u672c\u7167\u6284\u4ee3\u7801\u5373\u53ef<\/p>\n\n\n\n<p>\u4e00\u76f4\u7167\u6284\u5230Lv1.2\uff0c\u6211\u4eec\u6210\u529f\u5b9e\u73b0\u4e86hello.c<\/p>\n\n\n\n<p>\u63a5\u4e0b\u6765\u5b9e\u9645\u4e0a\u5c31\u662f\u628a\u4e4b\u524d\u4ee3\u7801\u4e2d\u7684string\u53d8\u6210\u4e86AST\u5bf9\u5e94\u7684\u7ed3\u6784\u4f53\uff0c\u5e76\u4e2d\u95f4\u5b9e\u73b0Dump()\u51fd\u6570\u8fdb\u884c\u8f93\u51fa\u64cd\u4f5c\u3002\u6211\u4eec\u53ef\u4ee5\u7167\u7740\u4f8b\u5b50\u5c06\u4e0b\u9762\u6240\u6709\u5757\u90fd\u6d3e\u751f\u4e00\u4e2aAST\u7684\u7ed3\u6784\u4f53\uff0c\u5b9e\u73b0\u5bf9\u5e94\u529f\u80fd\u3002<\/p>\n\n\n\n<p>\u540c\u65f6\u5bf9\u4e8eLv1.4\uff0c\u5373\u5c06\u5bf9\u5e94\u7684dump\u4fee\u6539\u4f7f\u5f97\u7b26\u5408\u8981\u6c42\uff0c\u7531\u4e8e\u76ee\u524d\u4ee3\u7801\u7b80\u5355\u6211\u4eec\u5c31\u8003\u8651\u5b9e\u73b0\u8981\u6c42\u64cd\u4f5c\uff0c\u540e\u7eed\u6269\u5c55\u53ef\u91cd\u65b0\u4fee\u6539\u3002\uff08\u8fd9\u91cc\u6211\u4fdd\u7559\u4e86\u751f\u6210ast\u7684dump<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream>\n#include &lt;memory>\nclass BaseAST {\n public:\n  virtual ~BaseAST() = default;\n\n  virtual void Dump() const = 0;\n  virtual void DumpAST() const = 0;\n};\n\nclass CompUnitAST : public BaseAST {\n public:\n  std::unique_ptr&lt;BaseAST> func_def;\n\n  void Dump() const override {\n    func_def->Dump();\n  }\n  void DumpAST() const override {\n    std::cout &lt;&lt; \"CompUnitAST { \";\n    func_def->DumpAST();\n    std::cout &lt;&lt; \" }\";\n  }\n};\n\nclass FuncDefAST : public BaseAST {\n public:\n  std::unique_ptr&lt;BaseAST> func_type;\n  std::string ident;\n  std::unique_ptr&lt;BaseAST> block;\n\n  void Dump() const override {\n    std::cout &lt;&lt; \"fun \";\n    std::cout &lt;&lt; \"@\" &lt;&lt; ident &lt;&lt; \"(): \";\n    func_type->Dump();\n    std::cout &lt;&lt; \"{ \" &lt;&lt; std::endl;\n    block->Dump();\n    std::cout &lt;&lt; \"} \" &lt;&lt; std::endl;\n  }\n  void DumpAST() const override {\n    std::cout &lt;&lt; \"FuncDefAST { \";\n    func_type->DumpAST();\n    std::cout &lt;&lt; \", \" &lt;&lt; ident &lt;&lt; \", \";\n    block->DumpAST();\n    std::cout &lt;&lt; \" }\";\n  }\n};\n\nclass FuncTypeAST : public BaseAST {\n public:\n  std::string type;\n  void Dump() const override {\n    std::cout &lt;&lt; \"i32\" &lt;&lt; \" \";\n  }\n  void DumpAST() const override {\n    std::cout &lt;&lt; \"FuncTypeAST { \";\n    std::cout &lt;&lt; type;\n    std::cout &lt;&lt; \" }\";\n  }\n};\n\nclass BlockAST : public BaseAST {\n public:\n  std::unique_ptr&lt;BaseAST> stmt;\n  void Dump() const override {\n    std::cout &lt;&lt; \"\\%entry\" &lt;&lt; \": \" &lt;&lt; std::endl;\n    stmt->Dump();\n    std::cout &lt;&lt; std::endl;\n  }\n  void DumpAST() const override {\n    std::cout &lt;&lt; \"BlockAST { \";\n    stmt->DumpAST();\n    std::cout &lt;&lt; \" }\";\n  }\n};\n\nclass StmtAST : public BaseAST {\n public:\n  int number;\n  void Dump() const override {\n    std::cout &lt;&lt; \"  ret \";\n    std::cout &lt;&lt; number;\n  }\n  void DumpAST() const override {\n    std::cout &lt;&lt; \"StmtAST { \";\n    std::cout &lt;&lt; number;\n    std::cout &lt;&lt; \" }\";\n  }\n};\n<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6837\u6211\u4eec\u6309\u7167\u4ed6\u8981\u6c42\u7684\u8f93\u51fa\u683c\u5f0f\u4fee\u6539main.cpp\uff0c\u5229\u7528freopen\u8f93\u51fa<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  freopen(output,\"w\",stdout);\n  ast->Dump();\n  fclose(stdout);<\/code><\/pre>\n\n\n\n<p>\u6b64\u65f6\u6211\u4eec\u5c31\u53ef\u4ee5\u8fdb\u884c\u6d4b\u8bd5\u4e86\uff0c\u4f46\u662f\u6211\u4eec\u53d1\u73b0\u5176\u4e2d\u4e00\u4e2acomment\u6d4b\u8bd5\u70b9\u6ca1\u6709\u901a\u8fc7\uff0c\u6211\u4eec\u9996\u5148\u6dfb\u52a0\u4e00\u4e0berror\u4fe1\u606f\uff0c\u53d1\u73b0\u6ca1\u6709\u6b63\u786e\u8bc6\u522b&#8217;\/&#8217;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void yyerror(std::unique_ptr&lt;BaseAST> &amp;ast, const char *s) {\n  \n    extern int yylineno;    \/\/ defined and maintained in lex\n    extern char *yytext;    \/\/ defined and maintained in lex\n    int len=strlen(yytext);\n    int i;\n    char buf&#91;512]={0};\n    for (i=0;i&lt;len;++i){\n        sprintf(buf,\"%s%d \",buf,yytext&#91;i]);\n    }\n    fprintf(stderr, \"ERROR: %s at symbol '%s' on line %d\\n\", s, buf, yylineno);\n\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u65f6\u53ef\u4ee5\u53d1\u73b0\u6211\u4eec\u5e94\u8be5\u5728.l\u6587\u4ef6\u4e2d\u8fdb\u884c\u6ce8\u91ca\u5757\u7684\u5339\u914d\u5e76\u5c06\u4ed6\u5ffd\u7565\uff0c\u8fd9\u6837\u767e\u5ea6\u4e86\u4e00\u4e2a\u5339\u914d\u65b9\u6848\uff0c\u6dfb\u52a0\u8fdb\u53bb\uff0c\u8fd9\u6837\u5c31\u901a\u8fc7\u4e86\u5168\u90e8\u6d4b\u8bd5\u70b9\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>BlockComment  \"\/*\"(?:&#91;^\\*]|\\*+&#91;^\\\/\\*])*\\*+\\\/<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u4e3a\u65b0\u542f\u7528\u7684lab\u5b9e\u8df5\u65b9\u6848\uff0c\u52a9\u6559\u5c0f\u54e5\u54e5\u5199\u4e86\u4e00\u4e2a\u975e\u5e38\u8be6\u7ec6\u7684\u4e0a\u624b\u6587\u6863https:\/\/pku-minic.githu [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[90,86,91],"class_list":["post-881","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-koopa","tag-lab","tag-91"],"_links":{"self":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/881","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=881"}],"version-history":[{"count":1,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/881\/revisions"}],"predecessor-version":[{"id":882,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/881\/revisions\/882"}],"wp:attachment":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=881"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=881"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=881"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}