{"id":341,"date":"2019-04-27T23:12:03","date_gmt":"2019-04-27T15:12:03","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=341"},"modified":"2019-06-27T23:51:15","modified_gmt":"2019-06-27T15:51:15","slug":"%e5%9f%ba%e4%ba%8ellvm%e5%ae%9e%e7%8e%b0gafq%e7%bc%96%e7%a8%8b%e8%af%ad%e8%a8%801","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=341","title":{"rendered":"\u57fa\u4e8eLLVM\u5b9e\u73b0gafq\u7f16\u7a0b\u8bed\u8a00(1)"},"content":{"rendered":"\n<p>\u5728\u5b9e\u73b0\u8bed\u8a00\u65f6\uff0c\u9996\u5148\u9700\u8981\u7684\u662f\u80fd\u591f\u5904\u7406\u6587\u672c\u6587\u4ef6\u5e76\u8bc6\u522b\u5b83\u6240\u8bf4\u7684\u5185\u5bb9\u3002\u4f20\u7edf\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u201c\u8bcd\u6cd5\u5206\u6790\u5668\u201d\uff08\u53c8\u540d\u201c\u626b\u63cf\u4eea\u201d\uff09\u5c06\u8f93\u5165\u5206\u89e3\u4e3a\u201c\u4ee4\u724c\u201d\u3002\u8bcd\u6cd5\u5206\u6790\u5668\u8fd4\u56de\u7684\u6bcf\u4e2a\u6807\u8bb0\u5305\u62ec\u6807\u8bb0\u4ee3\u7801\u548c\u53ef\u80fd\u7684\u4e00\u4e9b\u5143\u6570\u636e\uff08\u4f8b\u5982\uff0c\u6570\u5b57\u7684\u6570\u503c\uff09\u3002\u9996\u5148\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u53ef\u80fd\u6027\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>\/\/ The lexer returns tokens [0-255] if it is an unknown character, otherwise one<\/em>\n<em>\/\/ of these for known things.<\/em>\n<strong>enum<\/strong> Token {\n  tok_eof = -1,\n\n  <em>\/\/ commands<\/em>\n  tok_def = -2,\n  tok_extern = -3,\n\n  <em>\/\/ primary<\/em>\n  tok_identifier = -4,\n  tok_number = -5,\n};\n\n<strong>static<\/strong> std::string IdentifierStr; <em>\/\/ Filled in if tok_identifier<\/em>\n<strong>static<\/strong> double NumVal;             <em>\/\/ Filled in if tok_number<\/em><\/pre>\n\n\n\n<p>\u6211\u4eec\u7684\u8bcd\u6cd5\u5206\u6790\u5668\u8fd4\u56de\u7684\u6bcf\u4e2a\u6807\u8bb0\u5c06\u662fToken\u679a\u4e3e\u503c\u4e4b\u4e00\uff0c\u6216\u8005\u5b83\u5c06\u662f\u4e00\u4e2a&#8217;\u672a\u77e5&#8217;\u5b57\u7b26\uff0c\u5982&#8217;+&#8217;\uff0c\u5b83\u5c06\u4f5c\u4e3aASCII\u503c\u8fd4\u56de\u3002\u5982\u679c\u5f53\u524d\u4ee4\u724c\u662f\u6807\u8bc6\u7b26\uff0c\u5219IdentifierStr\u5168\u5c40\u53d8\u91cf\u4fdd\u5b58\u6807\u8bc6\u7b26\u7684\u540d\u79f0\u3002\u5982\u679c\u5f53\u524d\u6807\u8bb0\u662f\u6570\u5b57\u6587\u5b57\uff08\u59821.0\uff09\uff0c\u5219NumVal\u4fdd\u6301\u5176\u503c\u3002\u8bf7\u6ce8\u610f\uff0c\u4e3a\u7b80\u5355\u8d77\u89c1\uff0c\u6211\u4eec\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\uff0c\u8fd9\u4e0d\u662f\u771f\u6b63\u7684\u8bed\u8a00\u5b9e\u73b0\u7684\u6700\u4f73\u9009\u62e9:)\u3002\n\n\u8bcd\u6cd5\u5206\u6790\u5668\u7684\u5b9e\u9645\u5b9e\u73b0\u662f\u4e00\u4e2a\u540d\u4e3agettok\u7684\u51fd\u6570\u3002\u8c03\u7528gettok\u51fd\u6570\u4ee5\u4ece\u6807\u51c6\u8f93\u5165\u8fd4\u56de\u4e0b\u4e00\u4e2a\u6807\u8bb0\u3002\u5176\u5b9a\u4e49\u5f00\u59cb\u4e8e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>\/\/\/ gettok - Return the next token from standard input.<\/em>\n<strong>static<\/strong> int gettok() {\n  <strong>static<\/strong> int LastChar = ' ';\n\n  <em>\/\/ Skip any whitespace.<\/em>\n  <strong>while<\/strong> (isspace(LastChar))\n    LastChar = getchar();<\/pre>\n\n\n\n<p>gettok\u901a\u8fc7\u8c03\u7528C getchar\uff08\uff09\u51fd\u6570\u4ece\u6807\u51c6\u8f93\u5165\u4e00\u6b21\u8bfb\u53d6\u4e00\u4e2a\u5b57\u7b26\u6765\u5de5\u4f5c\u3002\u5b83\u5728\u8bc6\u522b\u5b83\u4eec\u65f6\u5403\u5b83\u4eec\u5e76\u5728LastChar\u4e2d\u5b58\u50a8\u8bfb\u53d6\u4f46\u672a\u5904\u7406\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u3002\u5b83\u5fc5\u987b\u505a\u7684\u7b2c\u4e00\u4ef6\u4e8b\u5c31\u662f\u5ffd\u7565\u4ee4\u724c\u4e4b\u95f4\u7684\u7a7a\u683c\u3002\u8fd9\u662f\u901a\u8fc7\u4e0a\u9762\u7684\u5faa\u73af\u5b8c\u6210\u7684\u3002\n\ngettok\u9700\u8981\u505a\u7684\u4e0b\u4e00\u4ef6\u4e8b\u662f\u8bc6\u522b\u6807\u8bc6\u7b26\u548c\u7279\u5b9a\u7684\u5173\u952e\u5b57\uff0c\u5982\u201cdef\u201d\u3002 Kaleidoscope\u901a\u8fc7\u8fd9\u4e2a\u7b80\u5355\u7684\u5faa\u73af\u5b9e\u73b0\u4e86\u8fd9\u4e00\u70b9\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>if<\/strong> (isalpha(LastChar)) { <em>\/\/ identifier: [a-zA-Z][a-zA-Z0-9]*<\/em>\n  IdentifierStr = LastChar;\n  <strong>while<\/strong> (isalnum((LastChar = getchar())))\n    IdentifierStr += LastChar;\n\n  <strong>if<\/strong> (IdentifierStr == \"def\")\n    <strong>return<\/strong> tok_def;\n  <strong>if<\/strong> (IdentifierStr == \"extern\")\n    <strong>return<\/strong> tok_extern;\n  <strong>return<\/strong> tok_identifier;\n}<\/pre>\n\n\n\n<p>\u8bf7\u6ce8\u610f\uff0c\u6b64\u4ee3\u7801\u5728\u8bbe\u7f6e\u6807\u8bc6\u7b26\u65f6\u8bbe\u7f6e\u201cIdentifierStr\u201d\u5168\u5c40\u3002\u6b64\u5916\uff0c\u7531\u4e8e\u8bed\u8a00\u5173\u952e\u5b57\u7531\u76f8\u540c\u7684\u5faa\u73af\u5339\u914d\uff0c\u6211\u4eec\u5728\u8fd9\u91cc\u5904\u7406\u5b83\u4eec\u3002\u6570\u503c\u7c7b\u4f3c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>if<\/strong> (isdigit(LastChar) || LastChar == '.') {   <em>\/\/ Number: [0-9.]+<\/em>\n  std::string NumStr;\n  <strong>do<\/strong> {\n    NumStr += LastChar;\n    LastChar = getchar();\n  } <strong>while<\/strong> (isdigit(LastChar) || LastChar == '.');\n\n  NumVal = strtod(NumStr.c_str(), 0);\n  <strong>return<\/strong> tok_number;\n}<\/pre>\n\n\n\n<p>\u8fd9\u662f\u5904\u7406\u8f93\u5165\u7684\u975e\u5e38\u7b80\u5355\u7684\u4ee3\u7801\u3002\u5f53\u4ece\u8f93\u5165\u8bfb\u53d6\u6570\u503c\u65f6\uff0c\u6211\u4eec\u4f7f\u7528C strtod\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u6211\u4eec\u5b58\u50a8\u5728NumVal\u4e2d\u7684\u6570\u503c\u3002\u8bf7\u6ce8\u610f\uff0c\u8fd9\u6ca1\u6709\u8fdb\u884c\u8db3\u591f\u7684\u9519\u8bef\u68c0\u67e5\uff1a\u5b83\u5c06\u9519\u8bef\u5730\u8bfb\u53d6\u201c1.23.45.67\u201d\u5e76\u50cf\u5904\u7406\u201c1.23\u201d\u4e00\u6837\u5904\u7406\u5b83\u3002\u968f\u610f\u6269\u5c55\u5b83:)\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u5904\u7406\u8bc4\u8bba\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>if<\/strong> (LastChar == '#') {\n  <em>\/\/ Comment until end of line.<\/em>\n  <strong>do<\/strong>\n    LastChar = getchar();\n  <strong>while<\/strong> (LastChar != EOF &amp;&amp; LastChar != '\\n' &amp;&amp; LastChar != '\\r');\n\n  <strong>if<\/strong> (LastChar != EOF)\n    <strong>return<\/strong> gettok();\n}<\/pre>\n\n\n\n<p>\u6211\u4eec\u901a\u8fc7\u8df3\u5230\u884c\u5c3e\u6765\u5904\u7406\u6ce8\u91ca\uff0c\u7136\u540e\u8fd4\u56de\u4e0b\u4e00\u4e2a\u4ee4\u724c\u3002\u6700\u540e\uff0c\u5982\u679c\u8f93\u5165\u4e0e\u4e0a\u8ff0\u60c5\u51b5\u4e4b\u4e00\u4e0d\u5339\u914d\uff0c\u5219\u5b83\u662f\u201c+\u201d\u4e4b\u7c7b\u7684\u8fd0\u7b97\u7b26\u5b57\u7b26\u6216\u6587\u4ef6\u7684\u7ed3\u5c3e\u3002\u8fd9\u4e9b\u4ee3\u7801\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5904\u7406\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">  <em>\/\/ Check for end of file.  Don't eat the EOF.<\/em>\n  <strong>if<\/strong> (LastChar == EOF)\n    <strong>return<\/strong> tok_eof;\n\n  <em>\/\/ Otherwise, just return the character as its ascii value.<\/em>\n  int ThisChar = LastChar;\n  LastChar = getchar();\n  <strong>return<\/strong> ThisChar;\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u5b9e\u73b0\u8bed\u8a00\u65f6\uff0c\u9996\u5148\u9700\u8981\u7684\u662f\u80fd\u591f\u5904\u7406\u6587\u672c\u6587\u4ef6\u5e76\u8bc6\u522b\u5b83\u6240\u8bf4\u7684\u5185\u5bb9\u3002\u4f20\u7edf\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u201c\u8bcd\u6cd5\u5206\u6790\u5668\u201d\uff08\u53c8\u540d\u201c\u626b\u63cf\u4eea\u201d\uff09<a href=\"https:\/\/www.ccagml.com\/?p=341\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">\u57fa\u4e8eLLVM\u5b9e\u73b0gafq\u7f16\u7a0b\u8bed\u8a00(1)<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[23],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/341"}],"collection":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=341"}],"version-history":[{"count":3,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/341\/revisions"}],"predecessor-version":[{"id":345,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/341\/revisions\/345"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=341"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}