{"id":2876,"date":"2013-08-15T09:46:31","date_gmt":"2013-08-15T00:46:31","guid":{"rendered":"http:\/\/peta.okechan.net\/blog\/?p=2876"},"modified":"2013-08-17T13:03:47","modified_gmt":"2013-08-17T04:03:47","slug":"1-3-%e5%ad%97%e5%8f%a5%e8%a7%a3%e6%9e%90%e5%99%a8","status":"publish","type":"post","link":"https:\/\/peta.okechan.net\/blog\/archives\/2876","title":{"rendered":"1.3. \u5b57\u53e5\u89e3\u6790\u5668"},"content":{"rendered":"<p><a href=\"https:\/\/peta.okechan.net\/blog\/archives\/2836\" title=\"LLVM\u306b\u3088\u308b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306e\u5b9f\u88c5\">LLVM\u306b\u3088\u308b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306e\u5b9f\u88c5\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u65e5\u672c\u8a9e\u8a33<\/a><br \/>\n\u7b2c1\u7ae0 \u4e07\u83ef\u93e1: \u307e\u3048\u304c\u304d\u3068\u5b57\u53e5\u89e3\u6790\u5668<br \/>\n<a href=\"http:\/\/llvm.org\/docs\/tutorial\/LangImpl01.html#the-lexer\" target=\"_blank\">\u7b2c3\u7bc0 \u5b57\u53e5\u89e3\u6790\u5668<\/a><\/p>\n<p>\u8a00\u8a9e\u306b\u307e\u305a\u5fc5\u8981\u306b\u306a\u308b\u306e\u304c\u3001\u30c6\u30ad\u30b9\u30c8\u30d5\u30a1\u30a4\u30eb\u3092\u51e6\u7406\u3057\u305d\u308c\u306b\u4f55\u304c\u66f8\u3044\u3066\u3042\u308b\u306e\u304b\u8a8d\u8b58\u3059\u308b\u80fd\u529b\u306e\u5b9f\u88c5\u3067\u3042\u308b\u3002<br \/>\n\u4f1d\u7d71\u7684\u306a\u65b9\u6cd5\u3067\u306f\u3001&#8221;\u5b57\u53e5\u89e3\u6790\u5668\uff08lexer\uff09&#8221;\uff08\u5225\u540d\u30b9\u30ad\u30e3\u30ca\u30fc\uff09\u3092\u4f7f\u3063\u3066\u30c6\u30ad\u30b9\u30c8\uff08\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\uff09\u3092\u30c8\u30fc\u30af\u30f3\u3054\u3068\u306b\u30d0\u30e9\u30d0\u30e9\u306b\u3059\u308b\u3002<br \/>\n\u5404\u30c8\u30fc\u30af\u30f3\u306f\u3001\u30c8\u30fc\u30af\u30f3\u30b3\u30fc\u30c9\u3068\u6f5c\u5728\u7684\u306a\u3044\u304f\u3064\u304b\u306e\u30e1\u30bf\u30c7\u30fc\u30bf\u3092\u4fdd\u6301\u3057\u3066\u3044\u308b\u5b57\u53e5\u89e3\u6790\u5668\u306b\u3088\u3063\u3066\u5f97\u3089\u308c\u308b\u3002\uff08\u4f8b\u3048\u3070\u6570\u5024\u578b\u306e\u6570\u5024\u3067\u3042\u308b\u30c8\u30fc\u30af\u30f3\u3001\u3068\u3044\u3063\u305f\u611f\u3058\u306b\u3002\uff09<br \/>\n\u307e\u305a\u3001&#8221;\u898b\u8fbc\u307f\uff08possibilities\uff09&#8221;\u3092\u5b9a\u7fa9\u3059\u308b\u3002<br \/>\n<em>\u8a33\u6ce8: \u3053\u308c\u304b\u3089\u30b3\u30fc\u30c9\u306e\u8aac\u660e\u306b\u5165\u3063\u3066\u3044\u304f\u304c\u3001\u3053\u3053\u3067\u8aac\u660e\u3057\u3066\u308b\u30b3\u30fc\u30c9\u306e\u5168\u6587\u306f<a href=\"http:\/\/llvm.org\/docs\/tutorial\/LangImpl02.html#full-code-listing\" target=\"_blank\">\u7b2c2\u7ae0\u306e\u6700\u5f8c<\/a>\u306b\u3042\u308b\u3002\u307e\u305f\u3001\u7b2c1\u7ae0\u3068\u7b2c2\u7ae0\u3067\u306f\u307e\u3060LLVM\u306f\u4f7f\u308f\u306a\u3044\u70b9\u306b\u6ce8\u610f\u3002<\/em><\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ \u672a\u77e5\u306e\u6587\u5b57\u306e\u5834\u5408\u3001\u5b57\u53e5\u89e3\u6790\u5668\u306f0\u4ee5\u4e0a255\u4ee5\u4e0b\u306e\u30c8\u30fc\u30af\u30f3\u5024\u3092\u8fd4\u3059\u3002\r\n\/\/ \u65e2\u77e5\u306e\u30c8\u30fc\u30af\u30f3\u306a\u3089\u3001\u305d\u306e\u30c8\u30fc\u30af\u30f3\u306b\u5408\u3063\u305f\u5024\u3092\u8fd4\u3059\u3002\r\nenum Token {\r\n  tok_eof = -1,\r\n\r\n  \/\/ \u30b3\u30de\u30f3\u30c9\r\n  tok_def = -2, tok_extern = -3,\r\n\r\n  \/\/ \u4e3b\u8981\u306a\u3082\u306e\uff08primary\uff09\r\n  tok_identifier = -4, tok_number = -5,\r\n};\r\n\r\nstatic std::string IdentifierStr;  \/\/ tok_identifier\u306e\u5834\u5408\u306b\u4ee3\u5165\u3055\u308c\u308b\r\nstatic double NumVal;              \/\/ tok_number\u306e\u5834\u5408\u306b\u4ee3\u5165\u3055\u308c\u308b<\/pre>\n<p>\u6211\u3005\u306e\u5b57\u53e5\u89e3\u6790\u5668\u306b\u3088\u3063\u3066\u8fd4\u3055\u308c\u308b\u5404\u30c8\u30fc\u30af\u30f3\u306f\u3001Token\u5217\u6319\u578b\u306e\u5024\u306e\u3069\u308c\u304b\u3001\u3082\u3057\u304f\u306f&#8221;+&#8221;\u306e\u3088\u3046\u306a\u4e0d\u660e\u306a\u6587\u5b57\u306e\u5834\u5408\u306b\u306f\u305d\u306eASCII\u30b3\u30fc\u30c9\u5024\u3092\u8fd4\u3059\u3002<br \/>\n\u3082\u3057\u73fe\u5728\u306e\u30c8\u30fc\u30af\u30f3\u304c\u8b58\u5225\u5b50\uff08identifier\uff09\u306a\u3089\u3001\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570IdentifierStr\u306f\u305d\u306e\u8b58\u5225\u5b50\u306e\u540d\u524d\u3092\u4fdd\u6301\u3059\u308b\u3002<br \/>\n\u3082\u3057\u73fe\u5728\u306e\u30c8\u30fc\u30af\u30f3\u304c\u6570\u5024\u30ea\u30c6\u30e9\u30eb\uff081.0\u306e\u3088\u3046\u306a\uff09\u306a\u3089\u3001NumVal\u306f\u305d\u306e\u5024\u3092\u4fdd\u6301\u3059\u308b\u3002<br \/>\n\u8aac\u660e\u3092\u7c21\u5358\u306b\u3059\u308b\u305f\u3081\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570\u3092\u4f7f\u3063\u3066\u308b\u4e8b\u306b\u6ce8\u610f\u3002<br \/>\n\u3053\u308c\u306f\u672c\u6c17\u306e\u8a00\u8a9e\u5b9f\u88c5\u3067\u306f\u304a\u3059\u3059\u3081\u3067\u304d\u306a\u3044\u65b9\u6cd5\u3067\u3042\u308b\u3002<\/p>\n<p>\u5b57\u53e5\u89e3\u6790\u5668\u306e\u5b9f\u88c5\u306f\u3001gettok\u95a2\u6570\u305f\u3060\u3072\u3068\u3064\u3060\u3051\u3067\u3042\u308b\u3002<br \/>\ngettok\u95a2\u6570\u306f\u3001\u6a19\u6e96\u5165\u529b\u304b\u3089\u6b21\u306e\u30c8\u30fc\u30af\u30f3\u3092\u5f97\u308b\u305f\u3081\u306b\u547c\u3070\u308c\u308b\u3002<br \/>\n\u305d\u306e\u5b9a\u7fa9\u306f\u6b21\u306e\u3088\u3046\u306b\u59cb\u307e\u308b\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/\/ gettok - \u6a19\u6e96\u5165\u529b\u304b\u3089\u6b21\u306e\u30c8\u30fc\u30af\u30f3\u3092\u8fd4\u3059\u3002\r\nstatic int gettok() {\r\n  static int LastChar = ' ';\r\n\r\n  \/\/ \u7a7a\u767d\u3092\u30b9\u30ad\u30c3\u30d7\u3059\u308b\u3002\r\n  while (isspace(LastChar))\r\n    LastChar = getchar();<\/pre>\n<p>gettok\u306fC\u306egetchar()\u95a2\u6570\u3092\u547c\u3076\u3053\u3068\u306b\u3088\u3063\u3066\u52d5\u4f5c\u3057\u3001\u4e00\u5ea6\u306b\u4e00\u6587\u5b57\u305a\u3064\u6a19\u6e96\u5165\u529b\u304b\u3089\u8aad\u307f\u8fbc\u3080\u3002<br \/>\ngettok\u306f\u6587\u5b57\u3092\u8aad\u307f\u8fbc\u307f\u3001\u305d\u3057\u3066\u305d\u308c\u3092\u8a8d\u8b58\u3057\u3001\u305d\u3057\u3066\u6700\u5f8c\u306b\u8aad\u3093\u3060\u6587\u5b57\u3092\uff08\u3057\u304b\u3057\u51e6\u7406\u306f\u305b\u305a\u306b\uff09LastChar\u306b\u4fdd\u5b58\u3059\u308b\u3002<br \/>\n\u6700\u521d\u306e\u4ed5\u4e8b\u306f\u3001\u30c8\u30fc\u30af\u30f3\u9593\u306e\u7a7a\u767d\u3092\u7121\u8996\u3059\u308b\u3053\u3068\u3067\u3042\u308b\u3002<br \/>\n\u4e0a\u8a18\u306e\u30eb\u30fc\u30d7\u306b\u3088\u3063\u3066\u3053\u308c\u304c\u884c\u308f\u308c\u308b\u3002<\/p>\n<p>gettok\u306e\u6b21\u306e\u4ed5\u4e8b\u306f\u3001\u8b58\u5225\u5b50\u3068&#8221;def&#8221;\u306e\u3088\u3046\u306a\u7279\u5b9a\u306e\u30ad\u30fc\u30ef\u30fc\u30c9\u306e\u8a8d\u8b58\u3067\u3042\u308b\u3002<br \/>\n\u4e07\u83ef\u93e1\u3067\u306f\u3053\u308c\u3092\u4ee5\u4e0b\u306e\u7c21\u5358\u306a\u30eb\u30fc\u30d7\u3067\u884c\u3046\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">if (isalpha(LastChar)) { \/\/ \u8b58\u5225\u5b50: &#x5B;a-zA-Z]&#x5B;a-zA-Z0-9]*\r\n  IdentifierStr = LastChar;\r\n  while (isalnum((LastChar = getchar())))\r\n    IdentifierStr += LastChar;\r\n\r\n  if (IdentifierStr == &quot;def&quot;) return tok_def;\r\n  if (IdentifierStr == &quot;extern&quot;) return tok_extern;\r\n  return tok_identifier;\r\n}<\/pre>\n<p>\u3053\u306e\u30b3\u30fc\u30c9\u306f\u3001\u8b58\u5225\u5b50\u3092\u5b57\u53e5\u89e3\u6790\u3057\u305f\u3089\u3059\u3050\u306b\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570IdentifierStr\u306b\u30bb\u30c3\u30c8\u3057\u3066\u308b\u4e8b\u306b\u6ce8\u610f\u3002<br \/>\n\u305d\u3057\u3066\u3001\u3053\u306e\u30eb\u30fc\u30d7\u306b\u3088\u3063\u3066\u4e88\u7d04\u8a9e\uff08language keyword\uff09\u306b\u5408\u81f4\u3057\u305f\u3089\u3001\u305d\u308c\u3092\u3053\u3053\u3067\u30a4\u30f3\u30e9\u30a4\u30f3\u3067\u51e6\u7406\u3059\u308b\u3002<br \/>\n\u6570\u5024\u306e\u5834\u5408\u3082\u4f3c\u3066\u3044\u308b\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">if (isdigit(LastChar) || LastChar == '.') {   \/\/ \u6570\u5024: &#x5B;0-9.]+\r\n  std::string NumStr;\r\n  do {\r\n    NumStr += LastChar;\r\n    LastChar = getchar();\r\n  } while (isdigit(LastChar) || LastChar == '.');\r\n\r\n  NumVal = strtod(NumStr.c_str(), 0);\r\n  return tok_number;\r\n}<\/pre>\n<p>\u3053\u308c\u3089\u5168\u3066\u3001\u5165\u529b\u3092\u51e6\u7406\u3059\u308b\u305f\u3081\u306e\u5b9f\u306b\u7d20\u76f4\u306a\u30b3\u30fc\u30c9\u3067\u3042\u308b\u3002<br \/>\n\u5165\u529b\u304b\u3089\u6570\u5024\u3092\u8aad\u307f\u53d6\u308b\u3068\u304d\u3001NumVal\u306b\u4fdd\u5b58\u3057\u305f\u6570\u5024\uff08\u306e\u6587\u5b57\u5217\uff09\u3092\u5909\u63db\u3059\u308b\u305f\u3081\u306bC\u306estrtod\u95a2\u6570\u3092\u4f7f\u7528\u3057\u3066\u3044\u308b\u3002<br \/>\n\u5341\u5206\u306a\u30a8\u30e9\u30fc\u30c1\u30a7\u30c3\u30af\u3092\u884c\u3063\u3066\u3044\u306a\u3044\u4e8b\u306b\u6ce8\u610f\u3002<br \/>\n\u3053\u306e\u307e\u307e\u3067\u306f&#8221;1.23.45.67&#8243;\u3092\u8aad\u307f\u8fbc\u3093\u3060\u6642\u306b&#8221;1.23&#8243;\u3068\u3057\u3066\u8aa4\u8a8d\u8b58\u3059\u308b\u3060\u308d\u3046\u3002<br \/>\n\u3053\u306e\u90e8\u5206\u3092\u6539\u826f\u3059\u308b\u306e\u306f\u7c21\u5358\u3060\u304b\u3089\u3084\u3063\u3066\u307f\u308b\u3068\u3044\u3044\u3002<\/p>\n<p>\u6b21\u306f\u30b3\u30e1\u30f3\u30c8\u306e\u51e6\u7406\u3067\u3042\u308b\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">if (LastChar == '#') {\r\n  \/\/ \u884c\u306e\u7d42\u308f\u308a\u307e\u3067\u304c\u30b3\u30e1\u30f3\u30c8\r\n  do LastChar = getchar();\r\n  while (LastChar != EOF &amp;&amp; LastChar != '\\n' &amp;&amp; LastChar != '\\r');\r\n\r\n  if (LastChar != EOF)\r\n    return gettok();\r\n}<\/pre>\n<p>\u884c\u306e\u7d42\u308f\u308a\u307e\u3067\u30b9\u30ad\u30c3\u30d7\u3057\u3001\u305d\u3057\u3066\u6b21\u306e\u30c8\u30fc\u30af\u30f3\u3092\u8fd4\u3059\u4e8b\u306b\u3088\u3063\u3066\u30b3\u30e1\u30f3\u30c8\u3092\u51e6\u7406\uff08\u7121\u8996\uff09\u3059\u308b\u3002<br \/>\n\u6700\u5f8c\u306b\u3001\u3082\u3057\u5165\u529b\u304c\u4ee5\u4e0a\u306e\u3044\u305a\u308c\u304b\u306b\u3082\u5408\u81f4\u3057\u306a\u3044\u5834\u5408\u3001\u305d\u308c\u306f&#8221;+&#8221;\u306e\u3088\u3046\u306a\u6f14\u7b97\u5b50\u3092\u8868\u3059\u6587\u5b57\u304b\u30d5\u30a1\u30a4\u30eb\u306e\u7d42\u308f\u308a\uff08EOF\uff09\u3067\u3042\u308b\u3002<br \/>\n\u305d\u308c\u3089\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u3066\u51e6\u7406\u3055\u308c\u308b\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">  \/\/ \u30d5\u30a1\u30a4\u30eb\u306e\u7d42\u308f\u308a\u3092\u30c1\u30a7\u30c3\u30af\u3059\u308b\u3002\r\n  if (LastChar == EOF)\r\n    return tok_eof;\r\n\r\n  \/\/ \u305d\u308c\u4ee5\u5916\u306e\u5834\u5408\u306b\u306f\u3001\u6587\u5b57\u306eASCII\u30b3\u30fc\u30c9\u5024\u3092\u305d\u306e\u307e\u307e\u8fd4\u3059\u3002\r\n  int ThisChar = LastChar;\r\n  LastChar = getchar();\r\n  return ThisChar;\r\n}<\/pre>\n<p>\u3053\u308c\u306b\u3088\u3063\u3066\u3001\u6211\u3005\u306f\u7c21\u5358\u306a\u4e07\u83ef\u93e1\u8a00\u8a9e\u306e\u305f\u3081\u306e\u5b8c\u5168\u306a\u5b57\u53e5\u89e3\u6790\u5668\u3092\u624b\u306b\u3057\u305f\u3002\uff08\u3053\u306e\u5b57\u53e5\u89e3\u6790\u5668\u306e\u30bd\u30fc\u30b9\u5168\u4f53\u306f\u6b21\u306e\u7ae0\u306e\u6700\u5f8c\u3067\u898b\u308c\u308b\u3002\uff09<br \/>\n\u6b21\u306f\u3001AST\uff08\u62bd\u8c61\u69cb\u6587\u6728\u3001Abstract Syntax Tree\uff09\u69cb\u7bc9\u306e\u305f\u3081\u306b\u3001\u3053\u306e\u5b57\u53e5\u89e3\u6790\u5668\u3092\u4f7f\u7528\u3057\u305f\u7c21\u5358\u306a\u69cb\u6587\u89e3\u6790\u5668\u3092\u69cb\u7bc9\u3057\u3066\u307f\u3088\u3046\u3002<br \/>\n\u305d\u306e\u306a\u304b\u3067\u3001\u5b57\u53e5\u89e3\u6790\u5668\u3068\u69cb\u6587\u89e3\u6790\u5668\u3092\u9023\u643a\u3055\u305b\u308b\u305f\u3081\u306e\u30c9\u30e9\u30a4\u30d0\u3082\u69cb\u7bc9\u3059\u308b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p><a href=\"https:\/\/peta.okechan.net\/blog\/archives\/2836\" title=\"LLVM\u306b\u3088\u308b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306e\u5b9f\u88c5\">LLVM\u306b\u3088\u308b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306e\u5b9f\u88c5\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u65e5\u672c\u8a9e\u8a33<\/a><br \/>\n\u7b2c1\u7ae0 \u4e07\u83ef\u93e1: \u307e\u3048\u304c\u304d\u3068\u5b57\u53e5\u89e3\u6790\u5668<br \/>\n<a href=\"http:\/\/llvm.org\/docs\/tutorial\/LangImpl01.html#the-lexer\" target=\"_blank\">\u7b2c3\u7bc0 \u5b57\u53e5\u89e3\u6790\u5668<\/a><\/p>\n<p>\u8a00\u8a9e\u306b\u307e\u305a\u5fc5\u8981\u306b\u306a\u308b\u306e\u304c\u3001\u30c6\u30ad\u30b9\u30c8\u30d5\u30a1\u30a4\u30eb\u3092\u51e6\u7406\u3057\u305d\u308c\u306b\u4f55\u304c\u66f8\u3044\u3066\u3042\u308b\u306e\u304b\u8a8d\u8b58\u3059\u308b\u80fd\u529b\u306e\u5b9f\u88c5\u3067\u3042\u308b\u3002<br \/>\n\u4f1d\u7d71\u7684\u306a\u65b9\u6cd5\u3067\u306f\u3001&#8221;\u5b57\u53e5\u89e3\u6790\u5668\uff08lexer\uff09&#8221;\uff08\u5225\u540d\u30b9\u30ad\u30e3\u30ca\u30fc\uff09\u3092\u4f7f\u3063\u3066\u30c6\u30ad\u30b9\u30c8\uff08\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\uff09\u3092\u30c8\u30fc\u30af\u30f3\u3054\u3068\u306b\u30d0\u30e9\u30d0\u30e9\u306b\u3059\u308b\u3002<br \/>\n\u5404\u30c8\u30fc\u30af\u30f3\u306f\u3001\u30c8\u30fc\u30af\u30f3\u30b3\u30fc\u30c9\u3068\u6f5c\u5728\u7684\u306a\u3044\u304f\u3064\u304b\u306e\u30e1\u30bf\u30c7\u30fc\u30bf\u3092\u4fdd\u6301\u3057\u3066\u3044\u308b\u5b57\u53e5\u89e3\u6790\u5668\u306b\u3088\u3063\u3066\u5f97\u3089\u308c\u308b\u3002\uff08\u4f8b\u3048\u3070\u6570\u5024\u578b\u306e\u6570\u5024\u3067\u3042\u308b\u30c8\u30fc\u30af\u30f3\u3001\u3068\u3044\u3063\u305f\u611f\u3058\u306b\u3002\uff09<br \/>\n\u307e\u305a\u3001&#8221;\u898b\u8fbc\u307f\uff08possibilities\uff09&#8221;\u3092\u5b9a\u7fa9\u3059\u308b\u3002<br \/>\n<em>\u8a33\u6ce8: \u3053\u308c\u304b\u3089\u30b3\u30fc\u30c9\u306e\u8aac\u660e\u306b\u5165\u3063\u3066\u3044\u304f\u304c\u3001\u3053\u3053\u3067\u8aac\u660e\u3057\u3066\u308b\u30b3\u30fc\u30c9\u306e\u5168\u6587\u306f<a href=\"http:\/\/llvm.org\/docs\/tutorial\/LangImpl02.html#full-code-listing\" target=\"_blank\">\u7b2c2\u7ae0\u306e\u6700\u5f8c<\/a>\u306b\u3042\u308b\u3002\u307e\u305f\u3001\u7b2c1\u7ae0\u3068\u7b2c2\u7ae0\u3067\u306f\u307e\u3060LLVM\u306f\u4f7f\u308f\u306a\u3044\u70b9\u306b\u6ce8\u610f\u3002<\/em><\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ \u672a\u77e5\u306e\u6587\u5b57\u306e\u5834\u5408\u3001\u5b57\u53e5\u89e3\u6790\u5668\u306f0\u4ee5\u4e0a255\u4ee5\u4e0b\u306e\u30c8\u30fc\u30af\u30f3\u5024\u3092\u8fd4\u3059\u3002\r\n\/\/ \u65e2\u77e5\u306e\u30c8\u30fc\u30af\u30f3\u306a\u3089\u3001\u305d\u306e\u30c8\u30fc\u30af\u30f3\u306b\u5408\u3063\u305f\u5024\u3092\u8fd4\u3059\u3002\r\nenum Token {\r\n  tok_eof = -1,\r\n\r\n  \/\/ \u30b3\u30de\u30f3\u30c9\r\n  tok_def = -2, tok_extern = -3,\r\n\r\n  \/\/ \u4e3b\u8981\u306a\u3082\u306e\uff08primary\uff09\r\n  tok_identifier = -4, tok_number = -5,\r\n};\r\n\r\nstatic std::string IdentifierStr;  \/\/ tok_identifier\u306e\u5834\u5408\u306b\u4ee3\u5165\u3055\u308c\u308b\r\nstatic double NumVal;              \/\/ tok_number\u306e\u5834\u5408\u306b\u4ee3\u5165\u3055\u308c\u308b<\/pre>\n<p>\u6211\u3005\u306e\u5b57\u53e5\u89e3\u6790\u5668\u306b\u3088\u3063\u3066\u8fd4\u3055\u308c\u308b\u5404\u30c8\u30fc\u30af\u30f3\u306f\u3001Token\u5217\u6319\u578b\u306e\u5024\u306e\u3069\u308c\u304b\u3001\u3082\u3057\u304f\u306f&#8221;+&#8221;\u306e\u3088\u3046\u306a\u4e0d\u660e\u306a\u6587\u5b57\u306e\u5834\u5408\u306b\u306f\u305d\u306eASCII\u30b3\u30fc\u30c9\u5024\u3092\u8fd4\u3059\u3002<br \/>\n\u3082\u3057\u73fe\u5728\u306e\u30c8\u30fc\u30af\u30f3\u304c\u8b58\u5225\u5b50\uff08identifier\uff09\u306a\u3089\u3001\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570IdentifierStr\u306f\u305d\u306e\u8b58\u5225\u5b50\u306e\u540d\u524d\u3092\u4fdd\u6301\u3059\u308b\u3002<br \/>\n\u3082\u3057\u73fe\u5728\u306e\u30c8\u30fc\u30af\u30f3\u304c\u6570\u5024\u30ea\u30c6\u30e9\u30eb\uff081.0\u306e\u3088\u3046\u306a\uff09\u306a\u3089\u3001NumVal\u306f\u305d\u306e\u5024\u3092\u4fdd\u6301\u3059\u308b\u3002<br \/>\n\u8aac\u660e\u3092\u7c21\u5358\u306b\u3059\u308b\u305f\u3081\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570\u3092\u4f7f\u3063\u3066\u308b\u4e8b\u306b\u6ce8\u610f\u3002<br \/>\n\u3053\u308c\u306f\u672c\u6c17\u306e\u8a00\u8a9e\u5b9f\u88c5\u3067\u306f\u304a\u3059\u3059\u3081\u3067\u304d\u306a\u3044\u65b9\u6cd5\u3067\u3042\u308b\u3002<\/p>\n<p>\u5b57\u53e5\u89e3\u6790\u5668\u306e\u5b9f\u88c5\u306f\u3001gettok\u95a2\u6570\u305f\u3060\u3072\u3068\u3064\u3060\u3051\u3067\u3042\u308b\u3002<br \/>\ngettok\u95a2\u6570\u306f\u3001\u6a19\u6e96\u5165\u529b\u304b\u3089\u6b21\u306e\u30c8\u30fc\u30af\u30f3\u3092\u5f97\u308b\u305f\u3081\u306b\u547c\u3070\u308c\u308b\u3002<br \/>\n\u305d\u306e\u5b9a\u7fa9\u306f\u6b21\u306e\u3088\u3046\u306b\u59cb\u307e\u308b\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/\/ gettok - \u6a19\u6e96\u5165\u529b\u304b\u3089\u6b21\u306e\u30c8\u30fc\u30af\u30f3\u3092\u8fd4\u3059\u3002\r\nstatic int gettok() {\r\n  static int LastChar = ' ';\r\n\r\n  \/\/ \u7a7a\u767d\u3092\u30b9\u30ad\u30c3\u30d7\u3059\u308b\u3002\r\n  while (isspace(LastChar))\r\n    LastChar = getchar();<\/pre>\n<p>gettok\u306fC\u306egetchar()\u95a2\u6570\u3092\u547c\u3076\u3053\u3068\u306b\u3088\u3063\u3066\u52d5\u4f5c\u3057\u3001\u4e00\u5ea6\u306b\u4e00\u6587\u5b57\u305a\u3064\u6a19\u6e96\u5165\u529b\u304b\u3089\u8aad\u307f\u8fbc\u3080\u3002<br \/>\ngettok\u306f\u6587\u5b57\u3092\u8aad\u307f\u8fbc\u307f\u3001\u305d\u3057\u3066\u305d\u308c\u3092\u8a8d\u8b58\u3057\u3001\u305d\u3057\u3066\u6700\u5f8c\u306b\u8aad\u3093\u3060\u6587\u5b57\u3092\uff08\u3057\u304b\u3057\u51e6\u7406\u306f\u305b\u305a\u306b\uff09LastChar\u306b\u4fdd\u5b58\u3059\u308b\u3002<br \/>\n\u6700\u521d\u306e\u4ed5\u4e8b\u306f\u3001\u30c8\u30fc\u30af\u30f3\u9593\u306e\u7a7a\u767d\u3092\u7121\u8996\u3059\u308b\u3053\u3068\u3067\u3042\u308b\u3002<br \/>\n\u4e0a\u8a18\u306e\u30eb\u30fc\u30d7\u306b\u3088\u3063\u3066\u3053\u308c\u304c\u884c\u308f\u308c\u308b\u3002<\/p>\n<p>gettok\u306e\u6b21\u306e\u4ed5\u4e8b\u306f\u3001\u8b58\u5225\u5b50\u3068&#8221;def&#8221;\u306e\u3088\u3046\u306a\u7279\u5b9a\u306e\u30ad\u30fc\u30ef\u30fc\u30c9\u306e\u8a8d\u8b58\u3067\u3042\u308b\u3002<br \/>\n\u4e07\u83ef\u93e1\u3067\u306f\u3053\u308c\u3092\u4ee5\u4e0b\u306e\u7c21\u5358\u306a\u30eb\u30fc\u30d7\u3067\u884c\u3046\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">if (isalpha(LastChar)) { \/\/ \u8b58\u5225\u5b50: &#x5B;a-zA-Z]&#x5B;a-zA-Z0-9]*\r\n  IdentifierStr = LastChar;\r\n  while (isalnum((LastChar = getchar())))\r\n    IdentifierStr += LastChar;\r\n\r\n  if (IdentifierStr == &quot;def&quot;) return tok_def;\r\n  if (IdentifierStr == &quot;extern&quot;) return tok_extern;\r\n  return tok_identifier;\r\n}<\/pre>\n<p>\u3053\u306e\u30b3\u30fc\u30c9\u306f\u3001\u8b58\u5225\u5b50\u3092\u5b57\u53e5\u89e3\u6790\u3057\u305f\u3089\u3059\u3050\u306b\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570IdentifierStr\u306b\u30bb\u30c3\u30c8\u3057\u3066\u308b\u4e8b\u306b\u6ce8\u610f\u3002<br \/>\n\u305d\u3057\u3066\u3001\u3053\u306e\u30eb\u30fc\u30d7\u306b\u3088\u3063\u3066\u4e88\u7d04\u8a9e\uff08language keyword\uff09\u306b\u5408\u81f4\u3057\u305f\u3089\u3001\u305d\u308c\u3092\u3053\u3053\u3067\u30a4\u30f3\u30e9\u30a4\u30f3\u3067\u51e6\u7406\u3059\u308b\u3002<br \/>\n\u6570\u5024\u306e\u5834\u5408\u3082\u4f3c\u3066\u3044\u308b\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">if (isdigit(LastChar) || LastChar == '.') {   \/\/ \u6570\u5024: &#x5B;0-9.]+\r\n  std::string NumStr;\r\n  do {\r\n    NumStr += LastChar;\r\n    LastChar = getchar();\r\n  } while (isdigit(LastChar) || LastChar == '.');\r\n\r\n  NumVal = strtod(NumStr.c_str(), 0);\r\n  return tok_number;\r\n}<\/pre>\n<p>\u3053\u308c\u3089\u5168\u3066\u3001\u5165\u529b\u3092\u51e6\u7406\u3059\u308b\u305f\u3081\u306e\u5b9f\u306b\u7d20\u76f4\u306a\u30b3\u30fc\u30c9\u3067\u3042\u308b\u3002<br \/>\n\u5165\u529b\u304b\u3089\u6570\u5024\u3092\u8aad\u307f\u53d6\u308b\u3068\u304d\u3001NumVal\u306b\u4fdd\u5b58\u3057\u305f\u6570\u5024\uff08\u306e\u6587\u5b57\u5217\uff09\u3092\u5909\u63db\u3059\u308b\u305f\u3081\u306bC\u306estrtod\u95a2\u6570\u3092\u4f7f\u7528\u3057\u3066\u3044\u308b\u3002<br \/>\n\u5341\u5206\u306a\u30a8\u30e9\u30fc\u30c1\u30a7\u30c3\u30af\u3092\u884c\u3063\u3066\u3044\u306a\u3044\u4e8b\u306b\u6ce8\u610f\u3002<br \/>\n\u3053\u306e\u307e\u307e\u3067\u306f&#8221;1.23.45.67&#8243;\u3092\u8aad\u307f\u8fbc\u3093\u3060\u6642\u306b&#8221;1.23&#8243;\u3068\u3057\u3066\u8aa4\u8a8d\u8b58\u3059\u308b\u3060\u308d\u3046\u3002<br \/>\n\u3053\u306e\u90e8\u5206\u3092\u6539\u826f\u3059\u308b\u306e\u306f\u7c21\u5358\u3060\u304b\u3089\u3084\u3063\u3066\u307f\u308b\u3068\u3044\u3044\u3002<\/p>\n<p>\u6b21\u306f\u30b3\u30e1\u30f3\u30c8\u306e\u51e6\u7406\u3067\u3042\u308b\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">if (LastChar == '#') {\r\n  \/\/ \u884c\u306e\u7d42\u308f\u308a\u307e\u3067\u304c\u30b3\u30e1\u30f3\u30c8\r\n  do LastChar = getchar();\r\n  while (LastChar != EOF &amp;&amp; LastChar != '\\n' &amp;&amp; LastChar != '\\r');\r\n\r\n  if (LastChar != EOF)\r\n    return gettok();\r\n}<\/pre>\n<p>\u884c\u306e\u7d42\u308f\u308a\u307e\u3067\u30b9\u30ad\u30c3\u30d7\u3057\u3001\u305d\u3057\u3066\u6b21\u306e\u30c8\u30fc\u30af\u30f3\u3092\u8fd4\u3059\u4e8b\u306b\u3088\u3063\u3066\u30b3\u30e1\u30f3\u30c8\u3092\u51e6\u7406\uff08\u7121\u8996\uff09\u3059\u308b\u3002<br \/>\n\u6700\u5f8c\u306b\u3001\u3082\u3057\u5165\u529b\u304c\u4ee5\u4e0a\u306e\u3044\u305a\u308c\u304b\u306b\u3082\u5408\u81f4\u3057\u306a\u3044\u5834\u5408\u3001\u305d\u308c\u306f&#8221;+&#8221;\u306e\u3088\u3046\u306a\u6f14\u7b97\u5b50\u3092\u8868\u3059\u6587\u5b57\u304b\u30d5\u30a1\u30a4\u30eb\u306e\u7d42\u308f\u308a\uff08EOF\uff09\u3067\u3042\u308b\u3002<br \/>\n\u305d\u308c\u3089\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u3066\u51e6\u7406\u3055\u308c\u308b\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">  \/\/ \u30d5\u30a1\u30a4\u30eb\u306e\u7d42\u308f\u308a\u3092\u30c1\u30a7\u30c3\u30af\u3059\u308b\u3002\r\n  if (LastChar == EOF)\r\n    return tok_eof;\r\n\r\n  \/\/ \u305d\u308c\u4ee5\u5916\u306e\u5834\u5408\u306b\u306f\u3001\u6587\u5b57\u306eASCII\u30b3\u30fc\u30c9\u5024\u3092\u305d\u306e\u307e\u307e\u8fd4\u3059\u3002\r\n  int ThisChar = LastChar;\r\n  LastChar = getchar();\r\n  return ThisChar;\r\n}<\/pre>\n<p>\u3053\u308c\u306b\u3088\u3063\u3066\u3001\u6211\u3005\u306f\u7c21\u5358\u306a\u4e07\u83ef\u93e1\u8a00\u8a9e\u306e\u305f\u3081\u306e\u5b8c\u5168\u306a\u5b57\u53e5\u89e3\u6790\u5668\u3092\u624b\u306b\u3057\u305f\u3002\uff08\u3053\u306e\u5b57\u53e5\u89e3\u6790\u5668\u306e\u30bd\u30fc\u30b9\u5168\u4f53\u306f\u6b21\u306e\u7ae0\u306e\u6700\u5f8c\u3067\u898b\u308c\u308b\u3002\uff09<br \/>\n\u6b21\u306f\u3001AST\uff08\u62bd\u8c61\u69cb\u6587\u6728\u3001Abstract Syntax Tree\uff09\u69cb\u7bc9\u306e\u305f\u3081\u306b\u3001\u3053\u306e\u5b57\u53e5\u89e3\u6790\u5668\u3092\u4f7f\u7528\u3057\u305f\u7c21\u5358\u306a\u69cb\u6587\u89e3\u6790\u5668\u3092\u69cb\u7bc9\u3057\u3066\u307f\u3088\u3046\u3002<br \/>\n\u305d\u306e\u306a\u304b\u3067\u3001\u5b57\u53e5\u89e3\u6790\u5668\u3068\u69cb\u6587\u89e3\u6790\u5668\u3092\u9023\u643a\u3055\u305b\u308b\u305f\u3081\u306e\u30c9\u30e9\u30a4\u30d0\u3082\u69cb\u7bc9\u3059\u308b\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[466,467],"class_list":["post-2876","post","type-post","status-publish","format-standard","hentry","category-tech","tag-kaleidoscope","tag-llvm"],"_links":{"self":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts\/2876","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/comments?post=2876"}],"version-history":[{"count":0,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts\/2876\/revisions"}],"wp:attachment":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/media?parent=2876"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/categories?post=2876"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/tags?post=2876"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}