{"id":342969,"date":"2021-02-15T07:27:22","date_gmt":"2021-02-14T23:27:22","guid":{"rendered":"http:\/\/4563.org\/?p=342969"},"modified":"2021-02-15T07:27:22","modified_gmt":"2021-02-14T23:27:22","slug":"c-%e8%af%ad%e8%a8%80%e7%ac%ac%e4%b8%89%e6%96%b9%e5%ba%93-melon-%e5%bc%80%e7%ae%b1%e5%8d%b3%e7%94%a8%e4%b9%8b%e8%af%8d%e6%b3%95%e5%88%86%e6%9e%90%e5%99%a8%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=342969","title":{"rendered":"C \u8bed\u8a00\u7b2c\u4e09\u65b9\u5e93 Melon \u5f00\u7bb1\u5373\u7528\u4e4b\u8bcd\u6cd5\u5206\u6790\u5668\u4f7f\u7528"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  C \u8bed\u8a00\u7b2c\u4e09\u65b9\u5e93 Melon \u5f00\u7bb1\u5373\u7528\u4e4b\u8bcd\u6cd5\u5206\u6790\u5668\u4f7f\u7528               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : monkeyNik <\/span>  <span><i><\/i> 1<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u4e4b\u524d\u7684\u6587\u7ae0\u4e2d\uff0c\u7b14\u8005\u4ecb\u7ecd\u4e86 Linux\/UNIX C \u8bed\u8a00\u5e93 Melon \u7684\u57fa\u672c\u529f\u80fd\u53ca\u6846\u67b6\u4f7f\u7528\u3002<\/p>\n<p>\u672c\u6587\u5c06\u4ecb\u7ecd Melon \u4e2d\u7684\u8bcd\u6cd5\u5206\u6790\u5668\u7ec4\u4ef6\u3002<\/p>\n<p>Melon \u7684 Github \u4ed3\u5e93\u4e3a\uff1a https:\/\/github.com\/Water-Melon\/Melon<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" referrerpolicy=\"no-referrer\" rel=\"noreferrer\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2021\/02\/20210219_602fdc74dd435.png\" alt=\"C \u8bed\u8a00\u7b2c\u4e09\u65b9\u5e93 Melon \u5f00\u7bb1\u5373\u7528\u4e4b\u8bcd\u6cd5\u5206\u6790\u5668\u4f7f\u7528\" \/><\/p>\n<p>\u8bcd\u6cd5\u5206\u6790\u5668\u5728 Melon \u4e2d\u5e76\u4e0d\u4f9d\u8d56\u4e8e\u81ea\u8eab\u6846\u67b6\uff0c\u56e0\u6b64\u53ef\u4ee5\u5728\u4e0d\u521d\u59cb\u5316\u6846\u67b6\u7684\u60c5\u51b5\u4e0b\u5373\u53ef\u4f7f\u7528\u3002<\/p>\n<h2>\u57fa\u7840\u4f7f\u7528<\/h2>\n<p>\u6211\u4eec\u5148\u6765\u770b\u4e00\u4e2a\u57fa\u672c\u4f8b\u5b50\uff1a<\/p>\n<pre><code>\/\/lexer.c  #include &lt;stdio.h&gt; #include \"mln_lex.h\"  MLN_DEFINE_TOKEN_TYPE_AND_STRUCT(static, mln_test, TEST); MLN_DEFINE_TOKEN(mln_test, TEST);  int main(int argc, char *argv[]) {     if (argc != 2) {         fprintf(stderr, \"Usage: %s file_pathn\", argv[0]);         return -1;     }      mln_string_t path;     mln_lex_t *lex = NULL;     struct mln_lex_attr lattr;     mln_test_struct_t *ts;      mln_string_nSet(&amp;path, argv[1], strlen(argv[1]));     lattr.pool = mln_alloc_init();     if (lattr.pool == NULL) {         fprintf(stderr, \"init memory pool failedn\");         return -1;     }     lattr.keywords = NULL;     lattr.hooks = NULL;     lattr.preprocess = 0;     lattr.padding = 0;     lattr.type = M_INPUT_T_FILE;     lattr.data = &amp;path;      mln_lex_initWithHooks(mln_test, lex, &amp;lattr);     if (lex == NULL) {         fprintf(stderr, \"lexer init failedn\");         return -1;     }      while (1) {         ts = mln_test_token(lex);         if (ts == NULL || ts-&gt;type == TEST_TK_EOF)             break;         write(STDOUT_FILENO, ts-&gt;text-&gt;data, ts-&gt;text-&gt;len);         printf(\" line:%u type:%dn\", ts-&gt;line, ts-&gt;type);     }      return 0; } <\/code><\/pre>\n<p>\u5982\u6b64\uff0c\u5373\u53ef\u5b8c\u6210\u4e00\u4e2a\u8bcd\u6cd5\u89e3\u6790\u5668\u7a0b\u5e8f\uff0c\u5b83\u8bfb\u53d6\u7a0b\u5e8f\u7684\u53c2\u6570\u6240\u6307\u5b9a\u7684\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u7136\u540e\u89e3\u6790\u6210\u8bcd\u7d20\uff0c\u5e76\u5c06\u5176\u6253\u5370\u51fa\u6765\u3002<\/p>\n<p>\u6211\u4eec\u6267\u884c\uff1a<\/p>\n<pre><code>$ .\/lexer lexer.c  \/ line:1 type:21 \/ line:1 type:21 lexer line:1 type:5 . line:1 type:20 c line:1 type:5 # line:3 type:9 include line:3 type:5 &lt; line:3 type:24 stdio line:3 type:5 . line:3 type:20 h line:3 type:5 &gt; line:3 type:26 ... <\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u5c06\u6211\u4eec\u7684\u793a\u4f8b C \u7a0b\u5e8f\u62c6\u89e3\u6210\u5404\u79cd\u8bcd\u7d20\uff0c\u5982\uff1a\/\uff0c#\uff0c&lt;\u7b49\u7b49\u3002<\/p>\n<h2>\u8fdb\u9636\u4f7f\u7528<\/h2>\n<p>\u4e0a\u9762\u7684\u4f8b\u5b50\u53ef\u4ee5\u770b\u5230\uff0c\u57fa\u7840\u7684\u8bcd\u6cd5\u89e3\u6790\u5668\u89e3\u6790\u51fa\u7684\u8bcd\u7d20\u8fc7\u4e8e\u7ec6\u788e\uff0c\u6709\u65f6\u6211\u4eec\u8fd8\u5e0c\u671b\u89e3\u6790\u5668\u652f\u6301\u6211\u4eec\u81ea\u5b9a\u4e49\u7684\u5173\u952e\u5b57\u3001\u81ea\u5b9a\u4e49\u683c\u5f0f\u7684\u6570\u636e\uff0c\u751a\u81f3\u662f\u4e00\u4e9b\u9884\u5904\u7406\u529f\u80fd\uff0c\u4f8b\u5982\u5f15\u5165\u5176\u4ed6\u6587\u4ef6\u7684\u5185\u5bb9\u89e3\u6790\u8bcd\u7d20\u3002<\/p>\n<p>\u90a3\u4e48\uff0c\u6211\u4eec\u5c31\u5c06\u4e0a\u9762\u7684\u4f8b\u5b50\u8fdb\u884c\u4e00\u756a\u4fee\u6539\uff1a<\/p>\n<pre><code>\/\/lexer.c  #include &lt;stdio.h&gt; #include \"mln_lex.h\"  mln_string_t keywords[] = {     mln_string(\"on\"),     mln_string(\"off\"),     mln_string(NULL) };  MLN_DEFINE_TOKEN_TYPE_AND_STRUCT(static, mln_test, TEST, TEST_TK_ON, TEST_TK_OFF, TEST_TK_STRING); MLN_DEFINE_TOKEN(mln_test, TEST, {TEST_TK_ON, \"TEST_TK_ON\"}, {TEST_TK_OFF, \"TEST_TK_OFF\"}, {TEST_TK_STRING, \"TEST_TK_STRING\"});  static inline int mln_get_char(mln_lex_t *lex, char c) {     if (c == '\\') {         char n;         if ((n = mln_lex_getAChar(lex)) == MLN_ERR) return -1;         switch ( n ) {             case '\"':                 if (mln_lex_putAChar(lex, n) == MLN_ERR) return -1;                 break;             case ''':                 if (mln_lex_putAChar(lex, n) == MLN_ERR) return -1;                 break;             case 'n':                 if (mln_lex_putAChar(lex, 'n') == MLN_ERR) return -1;                 break;             case 't':                 if (mln_lex_putAChar(lex, 't') == MLN_ERR) return -1;                 break;             case 'b':                 if (mln_lex_putAChar(lex, 'b') == MLN_ERR) return -1;                 break;             case 'a':                 if (mln_lex_putAChar(lex, 'a') == MLN_ERR) return -1;                 break;             case 'f':                 if (mln_lex_putAChar(lex, 'f') == MLN_ERR) return -1;                 break;             case 'r':                 if (mln_lex_putAChar(lex, 'r') == MLN_ERR) return -1;                 break;             case 'v':                 if (mln_lex_putAChar(lex, 'v') == MLN_ERR) return -1;                 break;             case '\\':                 if (mln_lex_putAChar(lex, '\\') == MLN_ERR) return -1;                 break;             default:                 mln_lex_setError(lex, MLN_LEX_EINVCHAR);                 return -1;         }     } else {         if (mln_lex_putAChar(lex, c) == MLN_ERR) return -1;     }     return 0; }  static mln_test_struct_t * mln_test_dblq_handler(mln_lex_t *lex, void *data) {     mln_lex_cleanResult(lex);     char c;     while ( 1 ) {         c = mln_lex_getAChar(lex);         if (c == MLN_ERR) return NULL;         if (c == MLN_EOF) {             mln_lex_setError(lex, MLN_LEX_EINVEOF);             return NULL;         }         if (c == '\"') break;         if (mln_get_char(lex, c) &lt; 0) return NULL;     }     return mln_test_new(lex, TEST_TK_STRING); }  int main(int argc, char *argv[]) {     if (argc != 2) {         fprintf(stderr, \"Usage: %s file_pathn\", argv[0]);         return -1;     }      mln_string_t path;     mln_lex_t *lex = NULL;     struct mln_lex_attr lattr;     mln_test_struct_t *ts;     mln_lex_hooks_t hooks;      memset(&amp;hooks, 0, sizeof(hooks));     hooks.dblq_handler = (lex_hook)mln_test_dblq_handler;      mln_string_nSet(&amp;path, argv[1], strlen(argv[1]));      lattr.pool = mln_alloc_init();     if (lattr.pool == NULL) {         fprintf(stderr, \"init pool failedn\");         return -1;     }     lattr.keywords = keywords;     lattr.hooks = &amp;hooks;     lattr.preprocess = 1;\/\/\u652f\u6301\u9884\u5904\u7406     lattr.padding = 0;     lattr.type = M_INPUT_T_FILE;     lattr.data = &amp;path;      mln_lex_initWithHooks(mln_test, lex, &amp;lattr);     if (lex == NULL) {         fprintf(stderr, \"lexer init failedn\");         return -1;     }      while (1) {         ts = mln_test_token(lex);         if (ts == NULL || ts-&gt;type == TEST_TK_EOF)             break;         write(STDOUT_FILENO, ts-&gt;text-&gt;data, ts-&gt;text-&gt;len);         printf(\" line:%u type:%dn\", ts-&gt;line, ts-&gt;type);     }      return 0; } <\/code><\/pre>\n<p>\u8fd9\u4e00\u6b21\uff0c\u6211\u4eec\u589e\u52a0\u5982\u4e0b\u529f\u80fd\uff1a<\/p>\n<ul>\n<li>\u652f\u6301\u5173\u952e\u5b57 <code>on<\/code>\u548c<code>off<\/code><\/li>\n<li>\u652f\u6301\u8bc6\u522b\u53cc\u5f15\u53f7\u6269\u4f4f\u7684\u5185\u5bb9\u4e3a\u5b57\u7b26\u4e32\u7c7b\u578b<\/li>\n<li>\u589e\u52a0\u4e86\u9884\u5904\u7406\u529f\u80fd\uff0c\u4f8b\u5982\u5f15\u5165\u5176\u4ed6\u6587\u4ef6\u5185\u5bb9<\/li>\n<\/ul>\n<p>\u751f\u6210\u53ef\u6267\u884c\u7a0b\u5e8f\uff1a<\/p>\n<pre><code>$ cc -o a a.c -I \/usr\/local\/melon\/include\/ -L \/usr\/local\/melon\/lib\/ -lmelon -lpthread <\/code><\/pre>\n<p>\u521b\u5efa\u4e24\u4e2a\u6d4b\u8bd5\u6587\u4ef6\uff1a<\/p>\n<h4>a.ini<\/h4>\n<pre><code>#include \"b.ini\" test_mode = on log_level = 'debug' proc_num = 10 <\/code><\/pre>\n<h4>b.ini<\/h4>\n<pre><code>conf_name = \"b.ini\" <\/code><\/pre>\n<p>\u8fd0\u884c\u6211\u4eec\u7684\u7a0b\u5e8f\u6765\u770b\u770b\u6548\u679c\uff1a<\/p>\n<pre><code>$ .\/lexer a.ini  conf_name line:1 type:5 = line:1 type:25 b.ini line:1 type:42 test_mode line:2 type:5 = line:2 type:25 on line:2 type:40 log_level line:3 type:5 = line:3 type:25 ' line:3 type:13 debug line:3 type:5 ' line:3 type:13 proc_num line:4 type:5 = line:4 type:25 10 line:4 type:2 <\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u5728 a.ini \u4e2d\u5199\u5165<code>include<\/code>\u7684\u90e8\u5206\uff0c\u662f b.ini \u6587\u4ef6\u5185\u5bb9\u89e3\u6790\u540e\u7684\u8bcd\u7d20\u3002\u5e76\u4e14<code>on<\/code>\u548c<code>off<\/code>\u90fd\u88ab\u6b63\u5e38\u89e3\u6790\u51fa\u6765\u4e86\u3002\u4e14\u5b57\u7b26\u4e32\u4e5f\u88ab\u6b63\u5e38\u5904\u7406\u51fa\u6765\u4e86\u3002<\/p>\n<p>Melon \u7684 Github \u4ed3\u5e93\u4e3a\uff1a https:\/\/github.com\/Water-Melon\/Melon<\/p>\n<p>\u611f\u8c22\u9605\u8bfb<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>5<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5281777\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : LeeReamond <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5927\u4f6c\uff0c\u5173\u6ce8\u4f60\u8fd9\u4e2a\u9879\u76ee\u6709\u4e00\u6bb5\u65f6\u95f4\u4e86\uff0c\u770b\u5230\u4f60\u5728 v2 \u51e0\u6b21\u53d1\u5e16\u3002\u6211\u60f3\u77e5\u9053\u8fd9\u4e2a\u6846\u67b6\u80fd\u4e0d\u80fd\u505a\u4e00\u4e9b\u5f15\u5165 python \u751f\u6001\u76f8\u5173\u7684\u6559\u5b66\uff0c\u56e0\u4e3a\u73b0\u5728\u5199\u7eaf c \u5f88\u5c11\uff0c\u57fa\u672c\u90fd\u662f\u5d4c\u5165\u5728 python \u6216\u8005 cython \u91cc\u5199\uff0c\u6211\u5173\u6ce8\u4f60\u8fd9\u4e2a\u9879\u76ee\u7684\u539f\u56e0\u4e5f\u662f\u7528\u5230\u67d0\u4e9b\u7b97\u6cd5\u6216\u6570\u636e\u7ed3\u6784\u7684\u65f6\u5019\u53ef\u4ee5\u5b66\u4e60\u53c2\u8003\u4e00\u4e0b\uff0c\u4f46\u662f\u6211\u6c34\u5e73\u6bd4\u8f83\u4f4e\uff0c\u5e76\u4e0d\u4f1a\u76f4\u63a5\u8c03\u7528\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5281778\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : monkeyNik <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @LeeReamond \u4e4b\u524d\u5230\u786e\u5b9e\u672a\u66fe\u60f3\u8fc7\u8fd9\u65b9\u9762\uff0c\u56e0\u4e3a\u4e4b\u524d\u7684\u60f3\u6cd5\u662f\u4ee5\u6846\u67b6\u4e3a\u4e3b\uff08\u5373\u4ee5 C \u4e3a\u4e3b\uff09\uff0c\u6846\u67b6\u5185\u7684\u811a\u672c\u8bed\u8a00\u662f\u8f85\u52a9\u3002\u4e0d\u8fc7\u4f60\u7684\u60f3\u6cd5\u5012\u662f\u53ef\u4ee5\u501f\u9274\u4e00\u4e0b\uff0c\u65b9\u4fbf\u7684\u8bdd\u53ef\u4ee5\u52a0\u4e2a\u597d\u53cb\u6df1\u5165\u4ea4\u6d41\u4e0b\uff0c\u6211\u4e5f\u5e0c\u671b\u8fd9\u4e2a\u6846\u67b6\u80fd\u591f\u5e94\u7528\u4e8e\u66f4\u591a\u7684\u573a\u666f\u4e2d\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5281779\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : monkeyNik <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @LeeReamond \u5fd8\u8bb0\u8bf4\u4e86 \u4f60\u53ef\u4ee5\u5728 QQ \u7fa4\u91cc\u9762\u627e\u5230\u6211\u54c8                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5281780\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : LeeReamond <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @monkeyNik \u4f60\u5728 gh \u91cc\u8d34\u51fa\u7684\u7fa4\u53f7\u63d0\u793a\u641c\u7d22\u4e0d\u5230\u7fa4\u53f7\uff0c\u53ef\u80fd\u6709\u4e9b\u95ee\u9898\u9700\u8981\u6392\u67e5\u4e00\u4e0b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5281781\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : monkeyNik <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @LeeReamond \u591a\u8c22\u544a\u77e5\u554a\uff0c\u4e00\u4e2a\u8bbe\u7f6e\u5fd8\u6539\u4e86\u2026\u73b0\u5728\u5df2\u7ecf\u53ef\u4ee5\u5566                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>C \u8bed\u8a00\u7b2c\u4e09\u65b9\u5e93 Melon \u5f00\u7bb1&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/342969"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=342969"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/342969\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=342969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=342969"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=342969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}