{"id":9519,"date":"2023-11-05T09:30:50","date_gmt":"2023-11-05T01:30:50","guid":{"rendered":"https:\/\/www.orczhou.com\/?p=9519"},"modified":"2024-08-13T08:50:39","modified_gmt":"2024-08-13T00:50:39","slug":"using-yacc-bison-for-grammer","status":"publish","type":"post","link":"https:\/\/www.orczhou.com\/index.php\/2023\/11\/using-yacc-bison-for-grammer\/","title":{"rendered":"flex\/bison\u7cfb\u52172\uff1a\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u8ba1\u7b97\u5668"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">\u5f15\u8a00<\/h4>\n\n\n\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\u5728\u524d\u9762\uff0c\u5df2\u7ecf\u5b8c\u6210\u4e86<a href=\"https:\/\/www.orczhou.com\/index.php\/2023\/11\/learning-flex-bison-01\/\" data-type=\"link\" data-id=\"https:\/\/www.orczhou.com\/index.php\/2023\/11\/learning-flex-bison-01\/\">\u4f7f\u7528Lex\/flex\u505a\u57fa\u7840\u7684\u8bcd\u6cd5\u89e3\u6790<\/a>\u3002\u63a5\u7740\uff0c\u5f00\u59cb\u8fd9\u4e2a\u7cfb\u5217\u7684\u7b2c\u4e8c\u90e8\u5206\uff0c\u4f7f\u7528flex\u548cbison\u5b8c\u6210\u4e00\u4e2a\u7b80\u5355\u7684\u8ba1\u7b97\u5668\u3002<\/p>\n\n\n\n<p>\u4e3a\u4e86\u7b80\u5316\u5b9e\u73b0\uff0c\u5c06\u6ce8\u610f\u529b\u653e\u5728\u7b80\u5355flex\u548cbison\u4f7f\u7528\u4e0a\uff0c\u8fd9\u91cc\u5bf9\u8ba1\u7b97\u5668\u505a\u4e86\u51e0\u4e2a\u7b80\u5316\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53ea\u652f\u6301\u52a0\u3001\u51cf\u3001\u4e58\u8ba1\u7b97\uff0c\u6682\u65f6\u4e0d\u652f\u6301\u9664\u6cd5\uff0c\u9664\u6cd5\u53ef\u80fd\u6d89\u53ca\u5230\u6d6e\u70b9\u7c7b\u578b\uff0c\u6545\u6682\u65f6\u5ffd\u7565<\/li>\n\n\n\n<li>\u4e0d\u8003\u8651\u6574\u6570\u6ea2\u51fa\u7684\u95ee\u9898\uff0c\u8fd9\u91cc\u4f7f\u7528int\u7c7b\u578b\uff08\u90a3\u4e48\u4ed6\u5b58\u50a8\u4e0e\u8ba1\u7b97\u8303\u56f4\u662f\u6709\u9650\u7684\uff09<\/li>\n<\/ul>\n\n\n\n<p>\u4e5f\u5c31\u662f\u8be5\u7a0b\u5e8f\u53ef\u4ee5\u8ba1\u7b97\u52a0\u6cd5\u3001\u51cf\u6cd5\u3001\u4e58\u6cd5\uff0c\u4ee5\u53ca\u4ed6\u4eec\u7684\u4efb\u610f\u7ec4\u5408\uff0c\u5982\uff1a <code>3+4\u3001 3+4*2\u3001 3+4*2-3\u3001 3+4*2-3*2<\/code><\/p>\n\n\n\n<p>\u540e\u7eed\uff0c\u8fd8\u5c06\u8003\u8651\u589e\u52a0\u66f4\u6025\u590d\u6742\u7684\u8ba1\u7b97\u5668\uff0c\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5b9e\u73b0\uff0c\u5e26\u6709\u53d8\u91cf\u7684\u8ba1\u7b97\u5668\u7a0b\u5e8f<\/li>\n\n\n\n<li>\u5b9e\u73b0\u5e26\u6765\u5faa\u73af\u3001\u5e26\u6709\u6761\u4ef6\u5224\u65ad\u7684\u7a0b\u5e8f<\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u91cc\u5148\u4ece\u7b80\u5355\u7684\u5f00\u59cb\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u521d\u6b21\u624b\u5199\u4e00\u4e2acal.l\u548ccal.y<\/h4>\n\n\n\n<p>\u8fd9\u662f\u5728vim\u4e2d\u5199\u51fa\u7684\u7b2c\u4e00\u904d\u4ee3\u7801\uff0c\u5305\u542b\u4e86\u8bcd\u6cd5\u6587\u4ef6cal.l\u548c\u8bed\u6cd5\u6587\u4ef6cal.y\uff0c\u8be6\u7ec6\u5982\u4e0b\u3002\u8fd9\u5176\u4e2d\u5f53\u7136\u662f\u6709\u5f88\u591a\u9519\u8bef\u7684\uff0c\u4e4b\u6240\u4ee5\uff0c\u4f9d\u65e7\u5199\u51fa\u6765\uff0c\u662f\u4e3a\u4e86\u548c\u6b63\u786e\u4ee3\u7801\u5bf9\u6bd4\uff0c\u4ee5\u6b64\u770b\u770b\u5bf9\u54ea\u4e9b\u6982\u5ff5\u7406\u89e3\u6709\u504f\u5dee\u3002\u5982\u679c\u4f60\u662f\u627e\u4e00\u4e2a\u6b63\u786e\u4f8b\u5b50\u7684\u8bdd\uff0c\u53ef\u4ee5\u8df3\u8fc7\u8fd9\u4e00\u6bb5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"c\" class=\"language-c line-numbers\">%{\n#inlcude &lt;stdlib.h&gt;\n%}\n\/* \u5341\u8fdb\u5236\u6574\u6570 *\/\n%token INTEGER\n\n%union { \n    int a;\n}\n\n\/* \u64cd\u4f5c\u7b26 + - * \/ *\/\n%token OPERATOR\n\n%%\nprogram:\n    program expression \\n { printf(\"%d\",$2); }   \/\/ \u8fd9\u91cc\u5c31\u662f\u4ee5\u56de\u8f66\u7ed3\u5c3e\uff0c\u4e5f\u53ef\u4ee5\u8003\u8651\u4f7f\u7528 = \u7ed3\u5c3e\n    |\n\nexpression:\n\t\t  INTEGER\n\t\t| expression '+' expression {$$ = $1 + $2}\n\t\t| expression '-' expression {$$ = $1 - $2}\n\t\t| expression '*' expression {$$ = $1 * $2}\n\ndec_integer: \n\t\tINTEGER\n\t\t\t{\n\t\t\t$$ = $1.a;\n\t\t\t}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u4e5f\u6709\u51e0\u4e2a\u5df2\u77e5\u7684\u95ee\u9898\uff0c\u4f8b\u5982\uff1a\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u6ca1\u6709\u5b9a\u4e49\uff0c\u4e5f\u5c31\u8bf44+3*2\u53ef\u80fd\u4f1a\u7b97\u621014\u3002\u6ca1\u9519\uff0c\u5982\u679c\u773c\u5c16\u7684\u8bdd\uff0c\u8fd8\u53d1\u73b0\u6709\u4e00\u4e9b\u62fc\u5199\u9519\u8bef\u3002<\/p>\n\n\n\n<p>\u63a5\u7740\u662fcal.l\u6587\u4ef6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"c\" class=\"language-c\">#inlcude &lt;stdlib.h&gt;\n#include \"y.tab.h\"\n\u200b\n%}\n[:digit:]+ {\n            yylval.a = atoi(yytext)\n            RETURN INTEGER;\n}<\/code><\/pre>\n\n\n\n<p>\u5f53\u7136\uff0c\u8fd9\u91cc\u9762\u6709\u5f88\u591a\u7684\u9519\u8bef\u3002\u4e00\u4f1a\u513f\u6765\u770b\u540e\u9762\u6b63\u786e\u7684\u5185\u5bb9\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4fee\u6539cal.l\u548ccal.y<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9996\u5148\uff0c\u662f\u53bb\u89e3\u51b3\u5df2\u77e5\u7684\u95ee\u9898\uff1a\u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u5982\u4f55\u53bb\u89e3\u51b3\uff1f\u5173\u4e8e\u4ec0\u4e48\u662f\u4f18\u5148\u7ea7\u3001\u4ec0\u4e48\u662f\u7ed3\u5408\u5f8b\u8fd9\u91cc\u4e0d\u505a\u8be6\u8ff0\uff0c\u8fd9\u91cc\u6709\u4e00\u7bc7\u6587\u7ae0\u8bb2\u5f97\u6bd4\u8f83\u7ec6\u81f4\uff0c\u51e0\u5e45\u56fe\u4e5f\u975e\u5e38\u76f4\u89c2\uff1a<a href=\"https:\/\/www.geeksforgeeks.org\/precedence-and-associativity-of-operators-in-python\/\">Precedence and Associativity of Operators in Python<\/a>\u3002\u867d\u7136\u662f\u4e0d\u540c\u7684\u8bed\u8a00\uff0c\u4f46\u610f\u601d\u662f\u4e00\u6837\u7684\u3002\u7406\u89e3\u8fd9\u4e2a\u70b9\u8fd8\u662f\u6bd4\u8f83\u91cd\u8981\u7684\uff0c\u4f8b\u5982\u5728\u5173\u7cfb\u578b\u6570\u636e\u5e93\u4e2d\uff0c\u4e4b\u524d\u6709\u9047\u5230\u8fc7\u8fd9\u6837\u7684\u6848\u4f8b\uff0c\u53ef\u4ee5\u601d\u8003\u4e00\u4e0b\u5982\u4e0b\u7684\u8868\u8fbe\u5f0f<code> t.col &lt; 2 or  t.col &lt; 11 and  t.col &gt; 4<\/code> \u662f\u4ec0\u4e48\u610f\u601d\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">-- \u731c\u6d4b\u4e00\u4e0b\uff0c\u5982\u4e0b\u7684 SELECT \u67e5\u8be2\u662f\u5426\u80fd\u591f\u8fd4\u56de\u8bb0\u5f55\uff1a\n\nCREATE TABLE t(col INT); \n\nINSERT INTO t values (1);\n\nWHERE t.col &lt; 2 or  t.col &lt; 11 and  t.col &gt; 4<\/code><\/pre>\n\n\n\n<p>\u626f\u8fdc\u4e86\uff0c\u518d\u56de\u5230cal.y\u6587\u4ef6\uff0c\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b\u9700\u8981\u8fdb\u884c\u5982\u4e0b\u4fee\u6539\uff0c\u4ee5\u5b9a\u4e49&#8221;*&#8221;\u4f18\u5148\u7ea7\u9ad8\u4e8e&#8221;+&#8221;\u3001&#8221;-&#8220;\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"c\" class=\"language-c\">%left '+' '-'\n%left '*'<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u7684\u4ee3\u7801\u5148\u540e\uff0c\u5c31\u5b9a\u4e49\u4e86\u4f18\u5148\u7ea7\uff1b\u4f18\u5148\u7ea7\u8d8a\u9ad8\uff0c\u5b9a\u4e49\u5728\u8d8a\u5728\u540e\u9762\uff1bleft\u8868\u793a\uff0c\u5de6\u7ed3\u5408\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9664\u4e86\u4f18\u5148\u7ea7\uff0c\u5728cal.y\u8bed\u6cd5\u89c4\u5219\u4e2d\u7684\u5b9a\u4e49\u90e8\u5206\uff0c\u5982\u679c\u6709\u5b57\u7b26\uff0c\u5e76\u6ca1\u6709\u4f7f\u7528\u5f15\u53f7\u3002\u4f8b\u5982\u4e0a\u9762\u7684cal.y\u7684\u7b2c17\u884c\u7684\\n\uff0c\u662f\u9700\u8981\u52a0\u4e0a\u5f15\u53f7\u7684\uff0c\u5373 <code>'\\n'<\/code> \u3002<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5bf9\u4e8ecal.y\u7684\u4e2d\u5b9a\u4e49\u7684\u8bed\u6cd5\u89c4\u5219\uff0c\u5e76\u6ca1\u6709\u5b9a\u4e49\u8fd4\u56de\u503c\u5b58\u50a8\u5728\u8054\u5408\u4f53\uff08YYSTYPE\uff0c\u4e5f\u5c31\u662f\u5982\u4e0b\u8fd9\u91cccal.y\u5b9a\u4e49\u7684\u552f\u4e00\u7684\u90a3\u4e2a\u8054\u5408\u4f53\uff09\u54ea\u4e2a\u7c7b\u578b\u4e2d\u3002\u4f8b\u5982\uff0c\u6ca1\u6709\u5b9a\u4e49&#8221;expression&#8221;\u8fd9\u4e2a\u8bed\u6cd5\u89c4\u5219\uff0c\u8fd4\u56de\u503c\u662f\u4f7f\u7528\u54ea\u4e2a\u503c\uff0c\u867d\u7136\u8fd9\u91cc\u7684\u8054\u5408\u4f53\u53ea\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7c7b\u578b\uff08\u5373int a\uff09\u3002\u5177\u4f53\u7684\uff0c\u6dfb\u52a0\u4e86\u5982\u4e0b\u4ee3\u7801\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"c\" class=\"language-c\">%token &lt;a&gt; INTEGER\n%type &lt;a&gt; expression<\/code><\/pre>\n\n\n\n<p>\u5b8c\u6210\u8fd9\u6837\u7684\u5b9a\u4e49\u540e\uff0c\u5728lex\u7684\u6587\u4ef6cal.l\u4e2d\uff0c\u5c31\u53ef\u4ee5\u5bf9yylval\u8fdb\u884c\u8d4b\u503c\uff0c\u5982\uff1a<code>yylval.a = atoi(yytext);<\/code> \u8fd9\u65f6\uff0c\u5bf9\u4e8eyacc\u6587\u4ef6\u4e2dcal.y\uff0c\u5982\u679c\u5f15\u7528\u8fd9\u4e2aTOKEN\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528$1\uff08\u6216\u8005\u662f$2\u3001$3\uff09\u6765\u5f15\u7528lex\u89e3\u6790\u540e\u7684\u8fd4\u56de\u503c\uff0c\u5982\uff1a<code>expression:   INTEGER { $$ = $1;}<\/code>\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e8e\u662f\uff0c\u91cd\u65b0\u4f7f\u7528\u4e86\u72ec\u7acb\u7684Token\u91cd\u65b0\u5b9a\u4e49\u4e86\u8fd0\u7b97\u7b26\uff0c\u5e76\u5b9a\u4e49\u4e86\u4f18\u5148\u7ea7\uff0c\u5982\u4e0b\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"c\" class=\"language-c\">cat cal.y\n...\n%token O_ADD O_MINUS O_MULTIPLY O_EQ\n\n%left O_ADD O_MINUS\n%left O_MULTIPLY \n\n%token &lt;a&gt; INTEGER\n%type &lt;a&gt; expression\n...\n\ncat cal.l\n...\n\"=\" { return O_EQ;};\n\"+\" { return O_ADD;};\n\"-\" { return O_MINUS;};\n\"*\" { return O_MULTIPLY;};\n...<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6ca1\u6709\u5b9a\u4e49 yyerror \u51fd\u6570\uff0c\u7a0b\u5e8f\u4e5f\u4f1a\u7f16\u8bd1\u4e0d\u8fc7\u53bb\uff0c\u4f1a\u62a5\u5982\u4e0b\u9519\u8bef\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">cal.tab.c:(.text+0x53b): undefined reference to `yyerror'<\/code><\/pre>\n\n\n\n<p>\u6309\u7167\u6587\u6863\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u6700\u7b80\u5355\u7684yerror\u51fd\u6570\uff08\u53c2\u8003\uff1a<a href=\"https:\/\/www.gnu.org\/software\/bison\/manual\/html_node\/Error-Reporting-Function.html\">The Error Reporting Function yyerror<\/a>\uff09\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"c\" class=\"language-c\">void\nyyerror (char const *s)\n{\n  fprintf (stderr, \"something error: %s\\n over\", s);\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5220\u9664\u4e86\u65e0\u6548\u7684dec_integer\u89c4\u5219\uff0c\u5982\u679c\u6709\u65e0\u6548\u7684\u89c4\u5219\uff0c\u4e5f\u4f1a\u5931\u8d25<\/li>\n\n\n\n<li>\u5c06[:digit:]\u4fee\u6539\u4e3a[0-9]+\u3002\u81f3\u4e8e\u4e3a\u4ec0\u4e48[:digit:]\u4e0d\u751f\u6548\uff0c\u540e\u9762\u505a\u4e86\u4e00\u4e9b\u641c\u7d22\u3002\u4e3a\u4e86\u907f\u514d\u6b67\u4e49\uff0c\u9700\u8981\u989d\u5916\u518d\u52a0\u4e00\u5c42\u4e2d\u62ec\u53f7\uff0c\u5199\u6cd5\u4e5f\u5c31\u662f[[:digit:]]\u3002\u8be6\u7ec6\u53c2\u8003\uff1a<a href=\"https:\/\/www.cs.virginia.edu\/~cr4bd\/flex-manual\/Patterns.html\" data-type=\"link\" data-id=\"https:\/\/www.cs.virginia.edu\/~cr4bd\/flex-manual\/Patterns.html\">Patterns@Lexical Analysis With Flex<\/a><\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b8c\u6574\u7684\u8ba1\u7b97\u5668\u7a0b\u5e8f\u6587\u4ef6cal.l cal.y<\/h4>\n\n\n\n<p>\u8865\u5145\u4e00\u4e2amain\u5165\u53e3\u51fd\u6570\uff0c\u4fee\u6539cal.l\u3001cal.y\u5373\u53ef\u3002<\/p>\n\n\n\n<p><strong>\u4fee\u6b63\u540e\u7684cal.l<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"c\" class=\"language-c\">%{\n    #include \"cal.tab.h\"\n    #include &lt;stdio.h&gt; \n%}\n%option noyywrap\n%%\n[0-9]+ {\n\t\t\tyylval.a = atoi(yytext);\n\t\t\treturn INTEGER;\n    \t   }\n\n\"=\" { return O_EQ;};\n\"+\" { return O_ADD;};\n\"-\" { return O_MINUS;};\n\"*\" { return O_MULTIPLY;};\n\n%%<\/code><\/pre>\n\n\n\n<p><strong>\u4fee\u6b63\u540e\u7684cal.y<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"c\" class=\"language-c\">%{\n\t#include &lt;stdlib.h&gt;\n\t#include &lt;stdio.h&gt;\n\n\nint main(){\n\tyyparse();\n}\n\nvoid\nyyerror (char const *s)\n{\n  fprintf (stderr, \"something error: %s\\n over\", s);\n}\n\n%}\n\n\n%union { \n    int a;\n}\n\n%token O_ADD O_MINUS O_MULTIPLY O_EQ\n%token &lt;a&gt; INTEGER\n\n%left O_ADD O_MINUS\n%left O_MULTIPLY \n%type &lt;a&gt; expression\n\n%%\nprogram:\n    |\n    program expression O_EQ { printf(\"result is : %d\",$2); }   \n;\nexpression:\n\t\t  INTEGER { $$ = $1;}\n\t\t| expression O_ADD expression {$$ = $1 + $3; }\n\t\t| expression O_MINUS expression {$$ = $1 - $3; }\n\t\t| expression O_MULTIPLY expression {$$ = $1 * $3;}\n;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u7f16\u8bd1\u4e0e\u6267\u884c<\/h4>\n\n\n\n<p>\u7136\u540e\uff0c\u5c31\u53ef\u4ee5\u751f\u6210c\u6587\u4ef6\u5e76\u7f16\u8bd1\u53ef\u6267\u884c\u6587\u4ef6\u4e86\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">lex cal.l \nbison -d cal.y \ngcc cal.tab.c lex.yy.c -o a.out\n.\/a.out\n4+3*2=\n\n\u4e5f\u53ef\u4ee5\u50cf\u8fd9\u6837\uff1a\nlex cal.l &amp;&amp; bison -d cal.y &amp;&amp; gcc cal.tab.c lex.yy.c -o a.out &amp;&amp; .\/a.out<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u9644\u5e26\u6ce8\u91ca\u8bf4\u660e\u7684cal.y\u6587\u4ef6<\/h4>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"c\" class=\"language-c\">%{                                    \/\/ %{ ... %}  \u5305\u542b\u4e86\u5b8c\u6574\u7684C\u8bed\u8a00\u4ee3\u7801        \n\t#include &lt;stdlib.h&gt;           \/\/ \u8fd9\u91cc\u5305\u542b\u9700\u8981\u7684\u5934\u6587\u4ef6\u3001\u5165\u53e3\u51fd\u6570main\n\t#include &lt;stdio.h&gt;            \/\/    \u4ee5\u53ca\u4e00\u4e2a\u7b80\u7b54\u7684yyerror\u51fd\u6570\n\n\nint main(){\n\tyyparse();\n}\n\nvoid\nyyerror (char const *s)\n{\n  fprintf (stderr, \"something error: %s\\n over\", s);\n}\n\n%}\n\n\n%union {                             \/\/ \u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u8054\u5408\u4f53\uff0c\u7528\u4e8e\u5b9a\u4e49\n    int a;                           \/\/ \u5404\u79cd\u4e0d\u540c\u7c7b\u578b\u7684Token\u6240\u8fd4\u56de\u7684\u6570\u636e\n}                                    \/\/ \u5e7f\u6cdb\u7684\u88abyylex\u4f7f\u7528\uff0c\u5e76\u7528\u4e8e\u4e0e.yy\u6587\u4ef6\u5171\u4eab\u6570\u636e\n                                     \/\/ \u4e5f\u5c31\u662f YYSTYPE \n\n%token O_ADD O_MINUS O_MULTIPLY O_EQ\n%token &lt;a&gt; INTEGER                   \/\/ \u8fd9\u91cc\u8868\u793aINTEGER(\u8fd9\u662f\u4e00\u4e2a\u88ablex\u8bc6\u522b\u7684token\uff09\n                                     \/\/ INTEGER\uff08\u88ablex\u8bc6\u522b\u7684token\u8fd4\u56de\u503c\u4e3aYYSTYPE.a\n%left O_ADD O_MINUS                  \/\/ \u8fd9\u91cc\u5b9a\u4e49 + -\u4e3a\u5de6\u7ed3\u5408\u5f8b\n%left O_MULTIPLY                     \/\/ \u8fd9\u91cc\u5b9a\u4e49 * \u4e3a\u5de6\u7ed3\u5408\u5f8b\uff0c\u5e76\u4e14\u4f18\u5148\u7ea7\u9ad8\u4e8e + -\n%type &lt;a&gt; expression                 \/\/ \u8fd9\u91cc\u5b9a\u4e49\u8bed\u6cd5\u89c4\u5219(grammer rule)expression\n                                     \/\/ \u7684\u8fd4\u56de\u503c\u4e3a YYSTYPE.a\n%%\nprogram:                             \/\/ \u8fd9\u662fstart symbol\uff0c\u6240\u6709\u7684program\u90fd\u9700\u8981\u7b26\u5408\u8be5\u5b9a\u4e49\n    |\n    program expression O_EQ { printf(\"result is : %d\",$2); }   \n;\nexpression:\n\t\t  INTEGER { $$ = $1;}\n\t\t| expression O_ADD expression {$$ = $1 + $3; }\n\t\t| expression O_MINUS expression {$$ = $1 - $3; }\n\t\t| expression O_MULTIPLY expression {$$ = $1 * $3;}\n;<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f15\u8a00 \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\u5728\u524d\u9762\uff0c\u5df2\u7ecf\u5b8c\u6210\u4e86\u4f7f\u7528Lex\/flex\u505a\u57fa\u7840\u7684\u8bcd\u6cd5\u89e3\u6790\u3002\u63a5\u7740\uff0c\u5f00\u59cb\u8fd9\u4e2a\u7cfb\u5217\u7684\u7b2c\u4e8c\u90e8\u5206\uff0c\u4f7f\u7528flex\u548cbison\u5b8c\u6210\u4e00\u4e2a\u7b80\u5355\u7684\u8ba1\u7b97\u5668\u3002 \u4e3a\u4e86\u7b80\u5316\u5b9e\u73b0\uff0c\u5c06\u6ce8\u610f\u529b\u653e\u5728\u7b80\u5355flex\u548cbison\u4f7f\u7528\u4e0a\uff0c\u8fd9\u91cc\u5bf9\u8ba1\u7b97\u5668\u505a\u4e86\u51e0\u4e2a\u7b80\u5316\uff1a \u4e5f\u5c31\u662f\u8be5\u7a0b\u5e8f\u53ef\u4ee5\u8ba1\u7b97\u52a0\u6cd5\u3001\u51cf\u6cd5\u3001\u4e58\u6cd5\uff0c\u4ee5\u53ca\u4ed6\u4eec\u7684\u4efb\u610f\u7ec4\u5408\uff0c\u5982\uff1a 3+4\u3001 3+4*2\u3001 3+4*2-3\u3001 3+4*2-3*2 \u540e\u7eed\uff0c\u8fd8\u5c06\u8003\u8651\u589e\u52a0\u66f4\u6025\u590d\u6742\u7684\u8ba1\u7b97\u5668\uff0c\u5305\u62ec\uff1a \u8fd9\u91cc\u5148\u4ece\u7b80\u5355\u7684\u5f00\u59cb\u3002 \u521d\u6b21\u624b\u5199\u4e00\u4e2acal.l\u548ccal.y \u8fd9\u662f\u5728vim\u4e2d\u5199\u51fa\u7684\u7b2c\u4e00\u904d\u4ee3\u7801\uff0c\u5305\u542b\u4e86\u8bcd\u6cd5\u6587\u4ef6cal.l\u548c\u8bed\u6cd5\u6587\u4ef6cal.y\uff0c\u8be6\u7ec6\u5982\u4e0b\u3002\u8fd9\u5176\u4e2d\u5f53\u7136\u662f\u6709\u5f88\u591a\u9519\u8bef\u7684\uff0c\u4e4b\u6240\u4ee5\uff0c\u4f9d\u65e7\u5199\u51fa\u6765\uff0c\u662f\u4e3a\u4e86\u548c\u6b63\u786e\u4ee3\u7801\u5bf9\u6bd4\uff0c\u4ee5\u6b64\u770b\u770b\u5bf9\u54ea\u4e9b\u6982\u5ff5\u7406\u89e3\u6709\u504f\u5dee\u3002\u5982\u679c\u4f60\u662f\u627e\u4e00\u4e2a\u6b63\u786e\u4f8b\u5b50\u7684\u8bdd\uff0c\u53ef\u4ee5\u8df3\u8fc7\u8fd9\u4e00\u6bb5\u3002 \u8fd9\u91cc\u4e5f\u6709\u51e0\u4e2a\u5df2\u77e5\u7684\u95ee\u9898\uff0c\u4f8b\u5982\uff1a\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u6ca1\u6709\u5b9a\u4e49\uff0c\u4e5f\u5c31\u8bf44+3*2\u53ef\u80fd\u4f1a\u7b97\u621014\u3002\u6ca1\u9519\uff0c\u5982\u679c\u773c\u5c16\u7684\u8bdd\uff0c\u8fd8\u53d1\u73b0\u6709\u4e00\u4e9b\u62fc\u5199\u9519\u8bef\u3002 \u63a5\u7740\u662fcal.l\u6587\u4ef6\uff1a \u5f53\u7136\uff0c\u8fd9\u91cc\u9762\u6709\u5f88\u591a\u7684\u9519\u8bef\u3002\u4e00\u4f1a\u513f\u6765\u770b\u540e\u9762\u6b63\u786e\u7684\u5185\u5bb9\u3002 \u4fee\u6539cal.l\u548ccal.y \u626f\u8fdc\u4e86\uff0c\u518d\u56de\u5230cal.y\u6587\u4ef6\uff0c\u4f18\u5148\u7ea7\u548c\u7ed3\u5408\u5f8b\u9700\u8981\u8fdb\u884c\u5982\u4e0b\u4fee\u6539\uff0c\u4ee5\u5b9a\u4e49&#8221;*&#8221;\u4f18\u5148\u7ea7\u9ad8\u4e8e&#8221;+&#8221;\u3001&#8221;-&#8220;\uff1a \u8fd9\u91cc\u7684\u4ee3\u7801\u5148\u540e\uff0c\u5c31\u5b9a\u4e49\u4e86\u4f18\u5148\u7ea7\uff1b\u4f18\u5148\u7ea7\u8d8a\u9ad8\uff0c\u5b9a\u4e49\u5728\u8d8a\u5728\u540e\u9762\uff1bleft\u8868\u793a\uff0c\u5de6\u7ed3\u5408\u3002 \u5b8c\u6210\u8fd9\u6837\u7684\u5b9a\u4e49\u540e\uff0c\u5728lex\u7684\u6587\u4ef6cal.l\u4e2d\uff0c\u5c31\u53ef\u4ee5\u5bf9yylval\u8fdb\u884c\u8d4b\u503c\uff0c\u5982\uff1ayylval.a = atoi(yytext); \u8fd9\u65f6\uff0c\u5bf9\u4e8eyacc\u6587\u4ef6\u4e2dcal.y\uff0c\u5982\u679c\u5f15\u7528\u8fd9\u4e2aTOKEN\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528$1\uff08\u6216\u8005\u662f$2\u3001$3\uff09\u6765\u5f15\u7528lex\u89e3\u6790\u540e\u7684\u8fd4\u56de\u503c\uff0c\u5982\uff1aexpression: INTEGER { $$ = $1;}\u3002 \u6309\u7167\u6587\u6863\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u6700\u7b80\u5355\u7684yerror\u51fd\u6570\uff08\u53c2\u8003\uff1aThe Error Reporting Function yyerror\uff09\uff0c\u5982\u4e0b\uff1a \u5b8c\u6574\u7684\u8ba1\u7b97\u5668\u7a0b\u5e8f\u6587\u4ef6cal.l cal.y \u8865\u5145\u4e00\u4e2amain\u5165\u53e3\u51fd\u6570\uff0c\u4fee\u6539cal.l\u3001cal.y\u5373\u53ef\u3002 \u4fee\u6b63\u540e\u7684cal.l \u4fee\u6b63\u540e\u7684cal.y \u7f16\u8bd1\u4e0e\u6267\u884c \u7136\u540e\uff0c\u5c31\u53ef\u4ee5\u751f\u6210c\u6587\u4ef6\u5e76\u7f16\u8bd1\u53ef\u6267\u884c\u6587\u4ef6\u4e86\uff1a \u9644\u5e26\u6ce8\u91ca\u8bf4\u660e\u7684cal.y\u6587\u4ef6<\/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":[4,137],"tags":[],"class_list":["post-9519","post","type-post","status-publish","format-standard","hentry","category-code-detail","category-learning-more"],"_links":{"self":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/9519","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=9519"}],"version-history":[{"count":46,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/9519\/revisions"}],"predecessor-version":[{"id":12355,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/9519\/revisions\/12355"}],"wp:attachment":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/media?parent=9519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/categories?post=9519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/tags?post=9519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}