{"id":9487,"date":"2023-10-03T14:38:00","date_gmt":"2023-10-03T06:38:00","guid":{"rendered":"https:\/\/www.orczhou.com\/?p=9487"},"modified":"2024-08-13T08:50:57","modified_gmt":"2024-08-13T00:50:57","slug":"learning-flex-bison-01","status":"publish","type":"post","link":"https:\/\/www.orczhou.com\/index.php\/2023\/10\/learning-flex-bison-01\/","title":{"rendered":"flex\/bison\u7cfb\u52171\uff1a\u4f7f\u7528Lex\/flex\u505a\u57fa\u7840\u7684\u8bcd\u6cd5\u89e3\u6790"},"content":{"rendered":"\n<p>\u8fd9\u662f\u4e00\u4e2a\u7cfb\u5217\u6587\u7ae0\uff0c\u65e8\u5728\u4e86\u89e3\u5982\u4f55\u4f7f\u7528Flex\/Lex\u548cYacc\/Bison\u8fdb\u884c\u8bcd\u6cd5\u548c\u8bed\u6cd5\u89e3\u6790\u3002\u8fd9\u4e2a\u7cfb\u5217\uff0c\u5206\u6210\u4e86\u51e0\u4e2a\u90e8\u5206\uff0c\u5305\u62ec<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>flex\u7684\u57fa\u672c\u7528\u6cd5<\/li>\n\n\n\n<li>\u4f7f\u7528flex\/bison\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u8ba1\u7b97\u5668<\/li>\n\n\n\n<li>\u5b9e\u73b0\u4e00\u4e2a\u5e26\u6709\u6761\u4ef6\u5224\u65ad\u4e0e\u5faa\u73af\u7684\u8ba1\u7b97\u7a0b\u5e8f<\/li>\n<\/ul>\n\n\n\n<p>\u4e86\u89e3\u8fd9\u4e2a\u7cfb\u5217\u9700\u8981\u4e00\u5b9a\u7684\u7f16\u8bd1\u539f\u7406\u77e5\u8bc6\u4f5c\u4e3a\u80cc\u666f\u77e5\u8bc6\uff0c\u4e86\u89e3\u7a0b\u5e8f\u5982\u4f55\u4ece\u5b57\u7b26\u4e32\u5148\u89e3\u6790\u6210Token\uff0c\u800c\u540e\u4f7f\u7528\u8bed\u6cd5\u89e3\u6790\u5668\u751f\u6210\u89e3\u6790\u6811\uff0c\u6700\u540e\u6267\u884c\u8be5\u89e3\u6790\u6811\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6982\u8ff0<\/h4>\n\n\n\n<p>lex\/flex\u53ef\u4ee5\u6309\u7167\u201c\u8bcd\u6cd5\u6587\u4ef6\u201d\u7684\u5b9a\u4e49\uff0c\u5c06\u6587\u672c\u89e3\u6790\u6210\u5355\u4e2a\u7684Token\uff0c\u7136\u540e\u901a\u8fc7\u6267\u884c\u201c\u8bcd\u6cd5\u6587\u4ef6\u201d\u4e2d\u5b9a\u4e49\u7684Action\u6765\u5b8c\u6210\u4e00\u4e9b\u64cd\u4f5c\uff0c\u4e00\u822c\uff0cflex\u7684\u8f93\u51fa\u4f1a\u901a\u8fc7\u51fd\u6570\/\u53d8\u91cf\u5c06\u7ed3\u679c\u4f20\u9012\u7ed9yacc\/bison\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u8bed\u6cd5\u89e3\u6790\u3002\u4e3a\u4e86\u7b80\u5316\uff0c\u672c\u6587\u5c06\u4ec5\u901a\u8fc7\u72ec\u7acb\u7684\u201c\u8bcd\u6cd5\u6587\u4ef6\u201d\u5b8c\u6210\u4e00\u4e9b\u64cd\u4f5c\uff0c\u4ee5\u4e86\u89e3flex\u7684\u57fa\u7840\u4f7f\u7528\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u5b8c\u6210\u7684\u7a0b\u5e8f\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u201ccount\u201d\u7a0b\u5e8f\uff0c\u8f93\u5165\u662f\u4e00\u4e2a\u6587\u4ef6\uff0c\u7a0b\u5e8f\u8f93\u51fa\u6587\u4ef6\u4e2d\u5305\u542b\u7684\u5b57\u7b26\u6570\u3001\u8bcd\u8bed\u6570\u3001\u4ee5\u53ca\u884c\u6570\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b89\u88c5flex\u5e76\u7f16\u5199\u8bcd\u6cd5\u6587\u4ef6<\/h4>\n\n\n\n<p>1. \u5b89\u88c5lex\uff1a <code>yum\/apt-get install flex<\/code><\/p>\n\n\n\n<p>2. \u7f16\u5199\u5982\u4e0b\u8bcd\u6cd5\u6587\u6863\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"c\" class=\"language-c\">%{                                       \/\/\n        int characters = 0;              \/\/    %{ ... }% \u4e4b\u95f4\u7684\u90e8\u5206\u662f\"Declarations\"\n        int words = 0;                   \/\/    Declarations \u90e8\u5206\u58f0\u660e\u7684\u53d8\u91cf\uff0c\u662f\u53ef\u4ee5\u5728\u5168\u5c40\u4f7f\u7528\u7684\n        int lines = 0;                   \/\/    \u4f8b\u5982\uff0c\u5728\u8be5\u793a\u4f8b\u7684main\u7a0b\u5e8f\u4e2d\uff0c\u5c31\u901a\u8fc7extern\u58f0\u660e\u7684\u65b9\u5f0f\n%}                                       \/\/    \u4f7f\u7528\u4e86\u8fd9\u4e9b\u53d8\u91cf\n%%                                       \/\/\n\\n      {                                \/\/    \u4ece\u8fd9\u91cc\u5f00\u59cb\u662fTranslations\u9636\u6bb5\n                ++lines;                 \/\/    \u8fd9\u91cc\u5b9a\u4e86Token\uff0c\u4ee5\u53ca\u9047\u5230\u4e86Token\u4e4b\u540e\n                ++characters;            \/\/    \u5e94\u8be5\u91c7\u53d6\/\u6267\u884c\u4ec0\u4e48\uff0c\u4f8b\u5982\u8fd9\u91cc\u9047\u5230\u4e86\\n\u5b57\u7b26\n        }                                \/\/    \u5219\uff0c\u5c06lines\/characters\u53d8\u91cf\u90fd\u52a01\n[ \\t]+          characters += yyleng;    \/\/\n[^ \\t\\n]+ {                              \/\/    \u6ce8\u91ca\u90e8\u5206\u7684\u6587\u672c\u9700\u8981\u5220\u9664\uff0c\u7a0b\u5e8f\u624d\u80fd\u6b63\u5e38\u7f16\u8bd1 \n                ++words;                 \/\/    \u5220\u9664\u6ce8\u91ca\u7684vim\u547d\u4ee4\uff1a1,$s\/\\\/\\\/.*$\/\/g \n                characters += yyleng;    \/\/\n        }                                \/\/\n                                         \/\/\n%%<\/code><\/pre>\n\n\n\n<p>\u76f4\u63a5\u4f7f\u7528\u5982\u4e0a\u4ee3\u7801\u7684\u8bdd\uff0c\u540e\u9762\u5c31\u4f1a\u5728gcc\u7f16\u8bd1\u7684\u65f6\u5019\u9047\u5230\u5982\u4e0b\u9519\u8bef\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">$ lex zzx.l\n$ gcc lex.yy.c wc.c -o wc.out\n\/tmp\/cc1SPYm2.o\uff1aIn function yylex':\nlex.yy.c:(.text+0x42f)\uff1aundefined reference toyywrap'\n\/tmp\/cc1SPYm2.o\uff1aIn function input':\nlex.yy.c:(.text+0xf73)\uff1aundefined reference toyywrap'\ncollect2: ld returned 1 exit status\n<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u4f60\u4e5f\u9047\u5230\u4e86\u8fd9\u4e2a\u9519\u8bef\uff0c\u4e0d\u7528\u62c5\u5fc3\uff0c\u4f60\u5e76\u4e0d\u5b64\u5355\uff0c\u5728Stackoverflow\u4e0a\u770b\u5230\u89e3\u51b3\u8be5\u5931\u8d25\u7684\u7684\u7b54\u6848\u4e00\u5171\u6709150\u70b9\u8d5e\uff08up\uff09\uff0c\u5c31\u77e5\u9053\u5927\u5bb6\u90fd\u4e00\u6837\u4e86\uff08<a href=\"https:\/\/stackoverflow.com\/questions\/1811125\/undefined-reference-to-yywrap\">\u53c2\u8003@Stackoverflow<\/a>\uff09\u3002\u56e0\u4e3a\u9ed8\u8ba4\u7684\uff0clex\u751f\u6210\u7684\u8bcd\u6cd5\u89e3\u6790\u7a0b\u5e8f\u4e2d\uff0c\u5728\u6700\u540e\u662f\u9700\u8981\u8c03\u7528\u7684yywrap\u51fd\u6570\u7684\uff08<a href=\"https:\/\/westes.github.io\/flex\/manual\/Generated-Scanner.html#index-yywrap_0028_0029\">\u5173\u4e8eyywrap<\/a>\uff09\uff0c\u5982\u679c\u4e0d\u6253\u7b97\u63d0\u4f9b\u8be5\u51fd\u6570\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528lex\u9009\u9879 <code>%option noyywrap<\/code> \u7981\u7528\u8be5\u8c03\u7528\u3002\u90a3\u4e48\u4e0a\u9762\u7684\u4ee3\u7801\u5c31\u9700\u8981\u4fee\u6539\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"c\" class=\"language-c\">$ cat zzx.l \n%{\n        int characters = 0;\n        int words = 0;\n        int lines = 0;\n%}\n%option noyywrap\n%%\n\\n      {\n                ++lines;\n                ++characters;\n        }\n[ \\t]+          characters += yyleng;\n[^ \\t\\n]+ {\n                ++words;\n                characters += yyleng;\n        }\n\n%%<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u7f16\u5199\u5165\u53e3\u51fd\u6570\u5e76\u8c03\u7528yylex<\/h4>\n\n\n\n<p>\u8bcd\u6cd5\u6587\u4ef6\u9700\u8981\u4f7f\u7528\u5de5\u5177flex\u5c06\u5176\u7f16\u8bd1\u751f\u6210\u4e00\u4e2ac\u8bed\u8a00\u6587\u4ef6\uff0c\u7136\u540e\u518d\u4f7f\u7528gcc\u5c06\u5176\u7f16\u8bd1\u6210\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\u3002\u7f16\u8bd1\u524d\uff0c\u6211\u4eec\u9700\u8981\u5148\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684main\u51fd\u6570. \u518d\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\u7684\u5165\u53e3\u51fd\u6570\uff08main\uff09\uff0c\u5e76\u8c03\u7528<code>yylex()<\/code>\u5c31\u53ef\u4ee5\u4e86\u3002\u5177\u4f53\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"c\" class=\"language-c\">$ cat wc.c\n#include &lt;stdio.h&gt;\n\nint yylex(void);\n\nint main(void)\n{\n        extern int characters, words, lines;\n\n        yylex();\n        printf(\"%d characters, \", characters);\n        printf(\"%d words, \", words);\n        printf(\"%d lines\\n\", lines);\n        return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\uff1a\u5728\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u8c03\u7528<code>yylex()<\/code>\u5b8c\u6210\u4e86\u5b9e\u9645\u7684\u8bcd\u6cd5\u89e3\u6790\u8fc7\u7a0b\uff0c\u5e76\u83b7\u5f97\u6267\u884c\u7ed3\u679c\u3002\u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5b9e\u9645\u8fc7\u7a0b\u6bd4\u8fd9\u8981\u66f4\u52a0\u590d\u6742\uff0c\u5728\u8bcd\u6cd5\u6587\u4ef6\u4e2d\uff0c\u6bcf\u4e00\u6b21rule\u89e3\u6790\u5b8c\u6210\u540e\uff0c\u518d\u8d77action\u90e8\u5206\uff0c\u901a\u5e38\u90fd\u4f1a\u6709<code>return<\/code>\u8bed\u53e5\u7ed3\u675f\u672c\u6b21yylex\u8c03\u7528\uff0c\u6240\u4ee5\u4f1a\u662f\u4e00\u4e2a\u53cd\u590d\u8c03\u7528yylex\u7684\u8fc7\u7a0b\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7f16\u8bd1\u5e76\u6267\u884c<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">$ lex zzx.l    \n$ gcc lex.yy.c wc.c -o wc.out\n$ chmod +x wc.out\n$ cat s.txt\nthis is a input file.\nthis is a input file.\n$ .\/wc.out &lt; zzx.l\n404 characters, 36 words, 18 lines\n$ .\/wc.out &lt; s.txt\n44 characters, 10 words, 2 lines<\/code><\/pre>\n\n\n\n<p>\u597d\u4e86\uff0c\u81f3\u6b64\uff0c\u6211\u4eec\u5c31\u5b8c\u6210\u4e00\u4e2a\u8bcd\u6cd5\u89e3\u6790\u7684\u4efb\u52a1\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u4efb\u52a1\u4e0d\u6d89\u53ca\u4efb\u4f55\u8bed\u6cd5\uff08yyac\uff09\u89e3\u6790\uff0c\u6240\u4ee5\u6bd4\u8f83\u9002\u5408\u521d\u5b66\u8005\u5b66\u4e60\u8bcd\u6cd5\u89e3\u6790\u5de5\u5177lex\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u8865\u5145\u5173\u4e8eDefinitions<\/h4>\n\n\n\n<p>\u4e3a\u4e86\u518d\u7565\u5fae\u589e\u5f3a\u8be5\u793a\u4f8b\u7684\uff0c\u8fd9\u91cc\u5bf9\u4e0a\u9762\u7684\u793a\u4f8b\u53c8\u505a\u4e86\u4e00\u4e2a\u5c0f\u8c03\u6574\uff0c\u65b0\u589e\u4e00\u884c\u201cDefinitions\u201d\uff0c\u6709\u65f6\u5019\u4e3a\u4e86\u589e\u5f3a\u53ef\u8bfb\u6027\uff0c\u4f1a\u5bf9\u4e00\u4e9bexpression\u5b9a\u4e49\u4e00\u4e2a\u540d\u79f0\uff0c\u5982\u4e0b\uff0c\u5c06\\n\u5b9a\u4e49\u4e3aNL\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"c\" class=\"language-c\">%{                                        \/\/\n        int characters = 0;               \/\/   %{ ... }% \u4e4b\u95f4\u7684\u90e8\u5206\u662f\"Declarations\"\n        int words = 0;                    \/\/   Declarations \u90e8\u5206\u58f0\u660e\u7684\u53d8\u91cf\uff0c\u662f\u53ef\u4ee5\u5728\u5168\u5c40\u4f7f\u7528\u7684\n        int lines = 0;                    \/\/   \u4f8b\u5982\uff0c\u5728\u8be5\u793a\u4f8b\u7684main\u7a0b\u5e8f\u4e2d\uff0c\u5c31\u901a\u8fc7extern\u58f0\u660e\u7684\u65b9\u5f0f\n%}                                        \/\/   \u4f7f\u7528\u4e86\u8fd9\u4e9b\u53d8\u91cf\n                                          \/\/\nNL \\n                                     \/\/   \u8fd9\u91cc\u65b0\u589e\u4e86\u4e00\u884c\uff0c\u8fd9\u662f\u4e00\u884c Definitions\n                                          \/\/   \u5c06\\n\u7528\u5b57\u6bcdNL\u5b9a\u4e49\uff0c\u6240\u4ee5\u4e0b\u9762\u7684\\n\u4e5f\u5c31\u53ef\u4ee5\u4f7f\u7528NL\n                                          \/\/   \u8bd5\u60f3\uff0c\u5982\u679c\u8868\u8fbe\u5f0f\u5f88\u590d\u6742\u7528\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u8bfb\u6027\u4f1a\u589e\u5f3a\u5f88\u591a\n%%                                        \/\/\nNL      {                                 \/\/   \u4ece\u8fd9\u91cc\u5f00\u59cb\u662fTranslations\u9636\u6bb5\n                ++lines;                  \/\/   \u8fd9\u91cc\u5b9a\u4e86Token\uff0c\u4ee5\u53ca\u9047\u5230\u4e86Token\u4e4b\u540e\n                ++characters;             \/\/   \u5e94\u8be5\u91c7\u53d6\/\u6267\u884c\u4ec0\u4e48\uff0c\u4f8b\u5982\u8fd9\u91cc\u9047\u5230\u4e86\\n\u5b57\u7b26\n        }                                 \/\/   \u5219\uff0c\u5c06lines\/characters\u53d8\u91cf\u90fd\u52a01\n[ \\t]+          characters += yyleng;     \/\/\n[^ \\t\\n]+ {                               \/\/   \u6ce8\u91ca\u90e8\u5206\u7684\u6587\u672c\u9700\u8981\u5220\u9664\uff0c\u7a0b\u5e8f\u624d\u80fd\u6b63\u5e38\u7f16\u8bd1        \n                ++words;                  \/\/   \u5220\u9664\u6ce8\u91ca\u7684vim\u547d\u4ee4\uff1a1,$s\/\\\/\\\/.*$\/\/g \n                characters += yyleng;     \/\/\n        }                                 \/\/\n                                          \/\/\n%%           <\/code><\/pre>\n\n\n\n<p>\u81ea\u6b64\uff0c\u6211\u4eec\u5c31\u4e86\u89e3\u4e00\u4e2a\u8bcd\u6cd5\u89e3\u6790\u6587\u4ef6\u7684\u51e0\u4e2a\u4e3b\u8981\u90e8\u5206\uff1aDefinitions\u3001Declarations\u3001rule\uff08\u4ee5\u53carule\u5bf9\u5e94\u7684Action\uff09\u3002<\/p>\n\n\n\n<p>\u53c2\u8003\u8d44\u6599\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.ibm.com\/docs\/en\/zos\/3.1.0?topic=tools-tutorial-using-lex-yacc\" data-type=\"link\" data-id=\"https:\/\/www.ibm.com\/docs\/en\/zos\/3.1.0?topic=tools-tutorial-using-lex-yacc\">Tutorial on using lex and yacc<\/a><\/li>\n\n\n\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/westes.github.io\/flex\/manual\/\" data-type=\"link\" data-id=\"https:\/\/westes.github.io\/flex\/manual\/\" target=\"_blank\">Lexical Analysis With Flex, for Flex 2.6.2<\/a> <\/li>\n\n\n\n<li><a href=\"https:\/\/tldp.org\/HOWTO\/Lex-YACC-HOWTO-6.html\" data-type=\"link\" data-id=\"https:\/\/tldp.org\/HOWTO\/Lex-YACC-HOWTO-6.html\">How do Lex and YACC work internally<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.gnu.org\/software\/bison\/manual\/html_node\/Token-Values.html\" data-type=\"link\" data-id=\"https:\/\/www.gnu.org\/software\/bison\/manual\/html_node\/Token-Values.html\">yylval@bison manul<\/a> <\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u66f4\u591a\u8bf4\u660e<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Flex \/ Lex\u7a0b\u5e8f\u901a\u5e38\u4e0eYacc\/Bison\u4e00\u8d77\u4f7f\u7528\uff0cflex\u8d1f\u8d23\u8bcd\u6cd5\u89e3\u6790\uff0cbison\u5219\u8d1f\u8d23\u8bed\u6cd5\u89e3\u6790<\/li>\n\n\n\n<li>flex\u4e0ebison\u63a5\u53e3\u7684\u51fd\u6570\uff0c\u5c31\u662f\u4e0a\u9762\u7684\u8c03\u7528 <code>yylex()<\/code>\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u6bcf\u6b21\u57fa\u4e8e\u67d0\u4e2a\u89c4\u5219\uff08rule\uff09\u89e3\u6790\u5230\u4e00\u4e2a\u65b0\u7684Token\u7684\u65f6\u5019\uff0c\u5219\u4f1a\u6267\u884c\u5bf9\u5e94\u7684\u201cAction\u201d\uff08\u4e5f\u5c31\u662f\u6bcf\u4e2aToken\u540e\u9762\u7684\u4ee3\u7801\uff09\u90e8\u5206\u7684\u4ee3\u7801\u3002\u4f8b\u5982\uff0c\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\u4f1a\u6267\u884c<code>++lines<\/code>\u6216<code>++words<\/code>\u4ee3\u7801\u3002<\/li>\n\n\n\n<li>\u5728rule action\u90e8\u5206\uff0c\u6211\u4eec\u770b\u5230\u4f7f\u7528\u4e86\u4e00\u4e2ayyleng\u7684\u201c\u53d8\u91cf\u201d\u7528\u4e8e\u83b7\u53d6\u5f53\u524d\u88ab\u89e3\u6790\u7684\u539f\u59cb\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u3002\u7c7b\u4f3c\u7684\u201c\u53d8\u91cf\u201d\u6709\uff1a<code>yyleng\u3001yytext\u3001yyin<\/code>\u7b49\uff0c\u5b8c\u6574\u7684\u5217\u8868\u53ef\u4ee5\u53c2\u8003\uff1a<a href=\"https:\/\/westes.github.io\/flex\/manual\/User-Values.html\" data-type=\"link\" data-id=\"https:\/\/westes.github.io\/flex\/manual\/User-Values.html\">Values Available To the User<\/a>\u3002\u53e6\u5916\uff0c\u8fd9\u4e9b\u201c\u53d8\u91cf\u201d\u5e76\u4e0d\u662f\u771f\u7684\u53d8\u91cf\uff0c\u5927\u90e8\u5206\u90fd\u662f\u4e00\u4e9b\u201c\u5b8f\u201d\uff0c\u4f8b\u5982\uff0cyytext\u7684\u771f\u5b9e\u5b9a\u4e49\u53ef\u80fd\u662f\u8fd9\u6837\u7684\uff1a<code>#define yytext (((struct yyguts_t*)yyscanner)-&gt;yytext_r)<\/code>\u3002\u4e86\u89e3\u8fd9\u4e00\u70b9\uff0c\u6709\u5229\u4e8e\u7406\u89e3\u8fd9\u4e9b&#8221;\u53d8\u91cf&#8221;\u5e76\u4e0d\u80fd\u5728\u5916\u90e8\u76f4\u63a5\u5f15\u7528\u3002\n<ul class=\"wp-block-list\">\n<li>yyin    yylex\u51fd\u6570\u5904\u7406\u7684\u5b57\u7b26\u4e32\u6765\u6e90\uff0c\u9ed8\u8ba4\u60c5\u51b5\u662f\u6807\u51c6\u8f93\u5165\uff0c\u5728\u4f60\u7684\u7a0b\u5e8f\uff0c\u4f8b\u5982\u53ef\u4ee5\u5b9a\u4e49\u4e3a\u4e00\u4e2a\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\uff08\u5728Posix\u4e2d\uff0c\u4e00\u8d77\u90fd\u662f\u6587\u4ef6\uff09<\/li>\n\n\n\n<li>yylength \u7528\u4e8e\u8bb0\u5f55\uff0c\u5f53\u524d\u8bfb\u53d6\u7684Token\u7684\u957f\u5ea6<\/li>\n\n\n\n<li>yytext  \u7528\u4e8e\u8bb0\u5f55\u5f53\u524d\u8bfb\u53d6\u7684\u6587\u672c<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u4e00\u822c\u7684\uff0cflex\u7684\u201cAction\u90e8\u5206\u201d\uff0c\u4f1a\u5305\u542b\u4e00\u4e2a<code>return<\/code>,\u4f8b\u5982\u5982\u679c\u9047\u5230\u4e00\u4e2a\u6574\u6570\uff0c\u53ef\u80fd\u4f1a\u770b\u5230\u7c7b\u4f3c\u8fd9\u6837\u7684\u4ee3\u7801\uff1a<code>return INTEGER;<\/code> \u8fd9\u65f6\u5019\uff0c<code>yylex()<\/code>\u9047\u5230\u4e00\u4e2a\u5bf9\u5e94\u7684\u5b57\u7b26\u5c31\u4f1a\u8fd4\u56de<code>INTEGER<\/code>\u3002<\/li>\n\n\n\n<li>\u5728\u5b9e\u8df5\u4e2d\uff0c\u5219\u662f\u6309\u7167\u5982\u4e0b\u65b9\u5f0f\u5b9e\u73b0\uff1a\n<ul class=\"wp-block-list\">\n<li>\u5728 yacc\/bison\u7684\u8bed\u6cd5\u6587\u4ef6\u4e2d\u5b9a\u4e49Token\uff0c\u4f8b\u5982\u6574\u6570\u4e3a <code>INTEGER<\/code>,\u8bed\u6cd5\u4e3a <code>%token INTEGER<\/code><\/li>\n\n\n\n<li>\u4f7f\u7528yacc\/bison\u547d\u4ee4\u751f\u6210\u5bf9\u5e94\u7684\u5934\u6587\u4ef6\uff0c\u5934\u6587\u4ef6\u5219\u4f1a\u5305\u542b\u5173\u4e8e <code>INTEGER<\/code>\u7684\u9884\u5b9a\u4e49\uff1a<code>#define INTEGER 257<\/code><\/li>\n\n\n\n<li>\u53ea\u9700\u8981\u5728flex\u8bcd\u6cd5\u6587\u4ef6\u4e2d\u5305\u542b\u8be5\u5934\u6587\u4ef6\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u8fd9\u91cc\u7684\u9884\u5b9a\u4e49 <code>INTEGER<\/code>\u4e86<\/li>\n\n\n\n<li>\u90a3\u4e48\u8f83\u4e3a\u5b8c\u6574\u7684\u4ee3\u7801\u770b\u8d77\u6765\u5c31\u662f\u8fd9\u6837<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code class=\"\">cat cal.y\n...\n%token INTEGER\n...<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code class=\"\">cat cal.tab.h \/\/\u8fd9\u662fbison\u751f\u6210\u7684\u6587\u4ef6\n...\n#define INTEGER 257\n...<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code class=\"\">cat cal.l\n...\n[[:digit:]]+  { return INTEGER; }\n...<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6211\u4eec\u5728\u8003\u8651\u53e6\u4e00\u4e2a\u95ee\u9898\uff1a\u5728lex\u7684rule action\u53ef\u4ee5\u4f7f\u7528\u672c\u5730\u7684\u201c\u53d8\u91cf\u201d\uff08\u5176\u5b9e\u662f\u201c\u5b8f\u201d\uff09\uff0c\u4e5f\u4f1a\u901a\u8fc7<code>return<\/code>\u8bed\u53e5\u7ed9yyparse()\u4e2d\u8c03\u7528yylex\u65f6\uff0c\u8fd4\u56de\u5f53\u524dToken\u7684\u7c7b\u578b\u3002\u5982\u679c\u4e00\u4e2aToken\u662f\u4e00\u4e2a<code>[:digit:]+<\/code>\u7684\u65f6\u5019\uff0c\u6211\u4eec\u9664\u4e86\u9700\u8981\u77e5\u9053\u8fd9\u4e2aToken\u662f\u4e00\u4e2a\u6574\u6570\u4e4b\u5916\uff0c\u81f3\u5c11yyparse()\u8fd8\u9700\u8981\u77e5\u9053\u8fd9\u4e2a\u662f\u4e00\u4e2a\u4ec0\u4e48\u6574\u6570\uff0c\u5177\u4f53\u6570\u503c\u662f\u591a\u5c11\uff0c\u5f53\u7136\u5e76\u4e0d\u662f\u6240\u6709\u7684token\u90fd\u9700\u8981\uff0c\u4e00\u822c<code>identifier<\/code>\u90fd\u662f\u9700\u8981\u7684\u3002\u800c\uff0c\u524d\u9762\u7684yytext\u90fd\u662fyylex\u672c\u5730\u7684\u201c\u53d8\u91cf\u201d\u3002\u8fd9\u65f6\u5019\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528<code>yylval<\/code>\u3002<code>yylval<\/code>\u662f\u4e00\u4e2a\u7531yacc\/bison\u5b9a\u4e49\u7684\u53d8\u91cf\uff08\u9ed8\u8ba4\u662fint\u7c7b\u578b\uff09\uff0c\u7528\u4e8e\u5b58\u50a8\u8bcd\u6cd5\u89e3\u6790\u9700\u8981\u4f20\u9012\u7ed9yyparse\u7684\u6570\u636e\uff0c\u5728yacc\/bison\u7684\u8bed\u53e5\u5904\u7406\u7684Action\u9636\u6bb5\uff0c\u53ef\u4ee5\u4f7f\u7528\u53d8\u91cf\uff0c\u4ee5\u83b7\u5f97\u8bcd\u6cd5\u89e3\u6790\u9636\u6bb5\u7684\u4e00\u4e9b\u503c\u3002\u4f8b\u5982\uff0c\u4e00\u4e2aToken\u662f\u4e00\u4e2a\u6574\u6570\u3001\u5b57\u7b26\u4e32\uff08\u5e76\u975ekeyword\uff09\u7684\u65f6\u5019\uff0c\u6211\u4eec\u4f1a\u5c06\u5bf9\u5e94\u7684\u503c\u5b58\u50a8\u5728yylval\u4e2d\u3002\u6240\u4ee5\uff0cyylval\u901a\u5e38\u4f1a\u88ab\u5b9a\u4e49\u4e3a\u4e00\u4e2a\u8054\u5408\u4f53\uff08union\u7c7b\u578b\uff09\uff0c\u7528\u4e8e\u5b58\u50a8\u4e0d\u540c\u7c7b\u578b\u7684\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p>\u5173\u4e8e\u8fd9\u51e0\u4e2a\u6982\u5ff5\u7684\u66f4\u8be6\u7ec6\u7ec6\u81f4\u7684\u89e3\u91ca\u53ef\u4ee5\u53c2\u8003\u6700\u524d\u9762\u63d0\u5230\u7684\u201cIBM\u7684z\/OS\u7cfb\u7edf\u7684\u6587\u6863\u4e2d\u5173\u4e8elex\u548cyacc\u7684\u4ecb\u7ecd\u201d\uff08\u53c2\u8003\uff1a<a href=\"https:\/\/www.ibm.com\/docs\/en\/zos\/3.1.0?topic=tools-tutorial-using-lex-yacc\" data-type=\"link\" data-id=\"https:\/\/www.ibm.com\/docs\/en\/zos\/3.1.0?topic=tools-tutorial-using-lex-yacc\">Tutorial on using lex and yacc<\/a>\uff09\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u662f\u4e00\u4e2a\u7cfb\u5217\u6587\u7ae0\uff0c\u65e8\u5728\u4e86\u89e3\u5982\u4f55\u4f7f\u7528Flex\/Lex\u548cYacc\/Bison\u8fdb\u884c\u8bcd\u6cd5\u548c\u8bed\u6cd5\u89e3\u6790\u3002\u8fd9\u4e2a\u7cfb\u5217\uff0c\u5206\u6210\u4e86\u51e0\u4e2a\u90e8\u5206\uff0c\u5305\u62ec \u4e86\u89e3\u8fd9\u4e2a\u7cfb\u5217\u9700\u8981\u4e00\u5b9a\u7684\u7f16\u8bd1\u539f\u7406\u77e5\u8bc6\u4f5c\u4e3a\u80cc\u666f\u77e5\u8bc6\uff0c\u4e86\u89e3\u7a0b\u5e8f\u5982\u4f55\u4ece\u5b57\u7b26\u4e32\u5148\u89e3\u6790\u6210Token\uff0c\u800c\u540e\u4f7f\u7528\u8bed\u6cd5\u89e3\u6790\u5668\u751f\u6210\u89e3\u6790\u6811\uff0c\u6700\u540e\u6267\u884c\u8be5\u89e3\u6790\u6811\u3002 \u6982\u8ff0 lex\/flex\u53ef\u4ee5\u6309\u7167\u201c\u8bcd\u6cd5\u6587\u4ef6\u201d\u7684\u5b9a\u4e49\uff0c\u5c06\u6587\u672c\u89e3\u6790\u6210\u5355\u4e2a\u7684Token\uff0c\u7136\u540e\u901a\u8fc7\u6267\u884c\u201c\u8bcd\u6cd5\u6587\u4ef6\u201d\u4e2d\u5b9a\u4e49\u7684Action\u6765\u5b8c\u6210\u4e00\u4e9b\u64cd\u4f5c\uff0c\u4e00\u822c\uff0cflex\u7684\u8f93\u51fa\u4f1a\u901a\u8fc7\u51fd\u6570\/\u53d8\u91cf\u5c06\u7ed3\u679c\u4f20\u9012\u7ed9yacc\/bison\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u8bed\u6cd5\u89e3\u6790\u3002\u4e3a\u4e86\u7b80\u5316\uff0c\u672c\u6587\u5c06\u4ec5\u901a\u8fc7\u72ec\u7acb\u7684\u201c\u8bcd\u6cd5\u6587\u4ef6\u201d\u5b8c\u6210\u4e00\u4e9b\u64cd\u4f5c\uff0c\u4ee5\u4e86\u89e3flex\u7684\u57fa\u7840\u4f7f\u7528\u3002 \u8fd9\u91cc\u5b8c\u6210\u7684\u7a0b\u5e8f\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u201ccount\u201d\u7a0b\u5e8f\uff0c\u8f93\u5165\u662f\u4e00\u4e2a\u6587\u4ef6\uff0c\u7a0b\u5e8f\u8f93\u51fa\u6587\u4ef6\u4e2d\u5305\u542b\u7684\u5b57\u7b26\u6570\u3001\u8bcd\u8bed\u6570\u3001\u4ee5\u53ca\u884c\u6570\u3002 \u5b89\u88c5flex\u5e76\u7f16\u5199\u8bcd\u6cd5\u6587\u4ef6 1. \u5b89\u88c5lex\uff1a yum\/apt-get install flex 2. \u7f16\u5199\u5982\u4e0b\u8bcd\u6cd5\u6587\u6863\uff1a \u76f4\u63a5\u4f7f\u7528\u5982\u4e0a\u4ee3\u7801\u7684\u8bdd\uff0c\u540e\u9762\u5c31\u4f1a\u5728gcc\u7f16\u8bd1\u7684\u65f6\u5019\u9047\u5230\u5982\u4e0b\u9519\u8bef\uff1a \u5982\u679c\u4f60\u4e5f\u9047\u5230\u4e86\u8fd9\u4e2a\u9519\u8bef\uff0c\u4e0d\u7528\u62c5\u5fc3\uff0c\u4f60\u5e76\u4e0d\u5b64\u5355\uff0c\u5728Stackoverflow\u4e0a\u770b\u5230\u89e3\u51b3\u8be5\u5931\u8d25\u7684\u7684\u7b54\u6848\u4e00\u5171\u6709150\u70b9\u8d5e\uff08up\uff09\uff0c\u5c31\u77e5\u9053\u5927\u5bb6\u90fd\u4e00\u6837\u4e86\uff08\u53c2\u8003@Stackoverflow\uff09\u3002\u56e0\u4e3a\u9ed8\u8ba4\u7684\uff0clex\u751f\u6210\u7684\u8bcd\u6cd5\u89e3\u6790\u7a0b\u5e8f\u4e2d\uff0c\u5728\u6700\u540e\u662f\u9700\u8981\u8c03\u7528\u7684yywrap\u51fd\u6570\u7684\uff08\u5173\u4e8eyywrap\uff09\uff0c\u5982\u679c\u4e0d\u6253\u7b97\u63d0\u4f9b\u8be5\u51fd\u6570\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528lex\u9009\u9879 %option noyywrap \u7981\u7528\u8be5\u8c03\u7528\u3002\u90a3\u4e48\u4e0a\u9762\u7684\u4ee3\u7801\u5c31\u9700\u8981\u4fee\u6539\u4e3a\uff1a \u7f16\u5199\u5165\u53e3\u51fd\u6570\u5e76\u8c03\u7528yylex \u8bcd\u6cd5\u6587\u4ef6\u9700\u8981\u4f7f\u7528\u5de5\u5177flex\u5c06\u5176\u7f16\u8bd1\u751f\u6210\u4e00\u4e2ac\u8bed\u8a00\u6587\u4ef6\uff0c\u7136\u540e\u518d\u4f7f\u7528gcc\u5c06\u5176\u7f16\u8bd1\u6210\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\u3002\u7f16\u8bd1\u524d\uff0c\u6211\u4eec\u9700\u8981\u5148\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684main\u51fd\u6570. \u518d\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\u7684\u5165\u53e3\u51fd\u6570\uff08main\uff09\uff0c\u5e76\u8c03\u7528yylex()\u5c31\u53ef\u4ee5\u4e86\u3002\u5177\u4f53\u5982\u4e0b\uff1a \u8fd9\u91cc\u9700\u8981\u6ce8\u610f\uff1a\u5728\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u8c03\u7528yylex()\u5b8c\u6210\u4e86\u5b9e\u9645\u7684\u8bcd\u6cd5\u89e3\u6790\u8fc7\u7a0b\uff0c\u5e76\u83b7\u5f97\u6267\u884c\u7ed3\u679c\u3002\u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5b9e\u9645\u8fc7\u7a0b\u6bd4\u8fd9\u8981\u66f4\u52a0\u590d\u6742\uff0c\u5728\u8bcd\u6cd5\u6587\u4ef6\u4e2d\uff0c\u6bcf\u4e00\u6b21rule\u89e3\u6790\u5b8c\u6210\u540e\uff0c\u518d\u8d77action\u90e8\u5206\uff0c\u901a\u5e38\u90fd\u4f1a\u6709return\u8bed\u53e5\u7ed3\u675f\u672c\u6b21yylex\u8c03\u7528\uff0c\u6240\u4ee5\u4f1a\u662f\u4e00\u4e2a\u53cd\u590d\u8c03\u7528yylex\u7684\u8fc7\u7a0b\u3002 \u7f16\u8bd1\u5e76\u6267\u884c \u597d\u4e86\uff0c\u81f3\u6b64\uff0c\u6211\u4eec\u5c31\u5b8c\u6210\u4e00\u4e2a\u8bcd\u6cd5\u89e3\u6790\u7684\u4efb\u52a1\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u4efb\u52a1\u4e0d\u6d89\u53ca\u4efb\u4f55\u8bed\u6cd5\uff08yyac\uff09\u89e3\u6790\uff0c\u6240\u4ee5\u6bd4\u8f83\u9002\u5408\u521d\u5b66\u8005\u5b66\u4e60\u8bcd\u6cd5\u89e3\u6790\u5de5\u5177lex\u3002 \u8865\u5145\u5173\u4e8eDefinitions \u4e3a\u4e86\u518d\u7565\u5fae\u589e\u5f3a\u8be5\u793a\u4f8b\u7684\uff0c\u8fd9\u91cc\u5bf9\u4e0a\u9762\u7684\u793a\u4f8b\u53c8\u505a\u4e86\u4e00\u4e2a\u5c0f\u8c03\u6574\uff0c\u65b0\u589e\u4e00\u884c\u201cDefinitions\u201d\uff0c\u6709\u65f6\u5019\u4e3a\u4e86\u589e\u5f3a\u53ef\u8bfb\u6027\uff0c\u4f1a\u5bf9\u4e00\u4e9bexpression\u5b9a\u4e49\u4e00\u4e2a\u540d\u79f0\uff0c\u5982\u4e0b\uff0c\u5c06\\n\u5b9a\u4e49\u4e3aNL\uff1a \u81ea\u6b64\uff0c\u6211\u4eec\u5c31\u4e86\u89e3\u4e00\u4e2a\u8bcd\u6cd5\u89e3\u6790\u6587\u4ef6\u7684\u51e0\u4e2a\u4e3b\u8981\u90e8\u5206\uff1aDefinitions\u3001Declarations\u3001rule\uff08\u4ee5\u53carule\u5bf9\u5e94\u7684Action\uff09\u3002 \u53c2\u8003\u8d44\u6599\uff1a \u66f4\u591a\u8bf4\u660e \u5173\u4e8e\u8fd9\u51e0\u4e2a\u6982\u5ff5\u7684\u66f4\u8be6\u7ec6\u7ec6\u81f4\u7684\u89e3\u91ca\u53ef\u4ee5\u53c2\u8003\u6700\u524d\u9762\u63d0\u5230\u7684\u201cIBM\u7684z\/OS\u7cfb\u7edf\u7684\u6587\u6863\u4e2d\u5173\u4e8elex\u548cyacc\u7684\u4ecb\u7ecd\u201d\uff08\u53c2\u8003\uff1aTutorial on using lex and yacc\uff09\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_eb_attr":"","inline_featured_image":false,"_tocer_settings":[],"footnotes":""},"categories":[137],"tags":[],"class_list":["post-9487","post","type-post","status-publish","format-standard","hentry","category-learning-more"],"_links":{"self":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/9487","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/comments?post=9487"}],"version-history":[{"count":21,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/9487\/revisions"}],"predecessor-version":[{"id":12346,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/9487\/revisions\/12346"}],"wp:attachment":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/media?parent=9487"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/categories?post=9487"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/tags?post=9487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}