{"id":49339,"date":"2024-12-02T08:35:44","date_gmt":"2024-12-02T00:35:44","guid":{"rendered":"https:\/\/fwq.ai\/blog\/49339\/"},"modified":"2024-12-02T08:35:44","modified_gmt":"2024-12-02T00:35:44","slug":"antlr%e8%af%ad%e6%b3%95%e6%8a%a5%e9%94%99%ef%bc%9a%e4%b8%ba%e4%bb%80%e4%b9%88%e6%88%91%e7%9a%84%e7%ae%80%e5%8d%95%e8%ae%a1%e7%ae%97%e5%99%a8%e8%af%ad%e6%b3%95%e6%97%a0%e6%b3%95%e8%af%86%e5%88%ab","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/49339\/","title":{"rendered":"ANTLR\u8bed\u6cd5\u62a5\u9519\uff1a\u4e3a\u4ec0\u4e48\u6211\u7684\u7b80\u5355\u8ba1\u7b97\u5668\u8bed\u6cd5\u65e0\u6cd5\u8bc6\u522b\u6570\u5b57\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>ANTLR\u8bed\u6cd5\u62a5\u9519\uff1a\u4e3a\u4ec0\u4e48\u6211\u7684\u7b80\u5355\u8ba1\u7b97\u5668\u8bed\u6cd5\u65e0\u6cd5\u8bc6\u522b\u6570\u5b57\uff1f<\/h1>\n<p><span style=\"color: #222222;, sans-serif;background-color: #FFFFFF\">\u5404\u4f4d\u5c0f\u4f19\u4f34\u4eec\uff0c\u5927\u5bb6\u597d\u5440\uff01\u770b\u770b\u4eca\u5929\u6211\u53c8\u7ed9\u5404\u4f4d\u5e26\u6765\u4e86\u4ec0\u4e48\u6587\u7ae0\uff1f\u672c\u6587\u6807\u9898<span style=\"color: #222222;, sans-serif;background-color: #FFFFFF\">\u662f<\/span><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aANTLR\u8bed\u6cd5\u62a5\u9519\uff1a\u4e3a\u4ec0\u4e48\u6211\u7684\u7b80\u5355\u8ba1\u7b97\u5668\u8bed\u6cd5\u65e0\u6cd5\u8bc6\u522b\u6570\u5b57\uff1f\u300b<\/span>\uff0c\u5f88\u660e\u663e\u662f\u5173\u4e8e<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u7684\u6587\u7ae0\u54c8\u54c8\u54c8\uff0c\u5176\u4e2d\u5185\u5bb9\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u5982\u679c\u80fd\u5e2e\u5230\u4f60\uff0c\u89c9\u5f97\u5f88\u4e0d\u9519\u7684\u8bdd\uff0c\u6b22\u8fce\u5404\u4f4d\u591a\u591a\u70b9\u8bc4\u548c\u5206\u4eab\uff01<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241130\/1732971645674b0c7dbc8bb.jpg\" class=\"aligncenter\" title=\"ANTLR\u8bed\u6cd5\u62a5\u9519\uff1a\u4e3a\u4ec0\u4e48\u6211\u7684\u7b80\u5355\u8ba1\u7b97\u5668\u8bed\u6cd5\u65e0\u6cd5\u8bc6\u522b\u6570\u5b57\uff1f\u63d2\u56fe\" alt=\"ANTLR\u8bed\u6cd5\u62a5\u9519\uff1a\u4e3a\u4ec0\u4e48\u6211\u7684\u7b80\u5355\u8ba1\u7b97\u5668\u8bed\u6cd5\u65e0\u6cd5\u8bc6\u522b\u6570\u5b57\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>antlr \u8bed\u6cd5\u62a5\u9519\u89e3\u6790<\/strong><\/p>\n<p>\u521a\u63a5\u89e6 antlr \u7684\u5f00\u53d1\u8005\u53ef\u80fd\u4f1a\u9047\u5230\u6700\u7b80\u5355\u7684\u8ba1\u7b97\u8bed\u6cd5\u62a5\u9519\u3002\u672c\u6587\u5c06\u6df1\u5165\u63a2\u7a76\u4e00\u4e2a\u7b80\u5355\u7684 antlr \u8bed\u6cd5\u793a\u4f8b\uff0c\u5e76\u89e3\u51b3\u9047\u5230\u7684\u8bed\u6cd5\u9519\u8bef\u3002<\/p>\n<p><strong>\u9519\u8bef\u4fe1\u606f<\/strong><\/p>\n<p>\u9884\u5904\u7406\u7684 antlr \u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>grammar expr;\n\nprog: expr eof;\nexpr: expr('*'|'\/') expr  #multiordiv\n    | expr('+'|'-') expr  #addorsub\n    | int     #lieteral\n    | '(' expr ')'   #single\n    ;\nnewline : [\\r\\n]+ -&amp;gt; skip;<\/pre>\n<p>\u8fd0\u884c\u540e\uff0c\u663e\u793a\u4ee5\u4e0b\u9519\u8bef\u6d88\u606f\uff1a<\/p>\n<pre>1:1 token recognition error at: '+'\n1:3 token recognition error at: '*'\n1:2 mismatched input '2' expecting {, '*', '\/', '+', '-'}<\/pre>\n<p><strong>\u539f\u56e0\u5206\u6790<\/strong><\/p>\n<p>\u8bed\u6cd5\u9519\u8bef\u6e90\u4e8e\u5bf9 int \u7b26\u53f7\u7684\u672a\u5b9a\u4e49\u3002\u5728\u7ed9\u5b9a\u7684\u8bed\u6cd5\u4e2d\uff0cint \u672a\u58f0\u660e\u4e3a\u4e00\u79cd\u4ee4\u724c\uff0c\u56e0\u6b64 antlr \u4e0d\u77e5\u9053\u5982\u4f55\u8bc6\u522b\u5b83\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u8981\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u6dfb\u52a0 int \u4ee4\u724c\u5b9a\u4e49\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<pre>INT:    ('0' | [1-9] (Digits? | '_'+ Digits));<\/pre>\n<p>\u6b64\u5b9a\u4e49\u5c06 int \u4ee4\u724c\u5b9a\u4e49\u4e3a\u4ee5 0 \u5f00\u5934\u6216\u4ee5 1-9 \u4e4b\u95f4\u7684\u4e00\u4e2a\u6570\u5b57\u5f00\u5934\u7684\u6570\u5b57\uff0c\u540e\u9762\u53ef\u4ee5\u662f\u4efb\u610f\u6570\u91cf\u7684\u6570\u5b57\u6216\u4e0b\u5212\u7ebf\u52a0\u6570\u5b57\u3002<\/p>\n<p><strong>\u7ed3\u679c<\/strong><\/p>\n<p>\u6dfb\u52a0 int \u5b9a\u4e49\u540e\uff0cantlr \u5c06\u53ef\u4ee5\u8bc6\u522b int \u4ee4\u724c\uff0c\u5e76\u89e3\u6790\u8bed\u6cd5 \u062f\u0648\u0646\u51fa\u73b0\u9519\u8bef\u3002<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ANTLR\u8bed\u6cd5\u62a5\u9519\uff1a\u4e3a\u4ec0\u4e48\u6211\u7684\u7b80\u5355\u8ba1\u7b97\u5668\u8bed\u6cd5\u65e0\u6cd5\u8bc6\u522b\u6570\u5b57\uff1f \u5404\u4f4d\u5c0f\u4f19\u4f34\u4eec\uff0c\u5927\u5bb6\u597d\u5440\uff01\u770b\u770b\u4eca\u5929\u6211\u53c8\u7ed9\u5404\u4f4d\u5e26\u6765\u4e86\u4ec0\u4e48\u6587\u7ae0\uff1f\u672c\u6587\u6807\u9898\u662f\u300aANTLR\u8bed\u6cd5\u62a5\u9519\uff1a\u4e3a\u4ec0\u4e48\u6211\u7684\u7b80\u5355\u8ba1\u7b97\u5668\u8bed\u6cd5\u65e0\u6cd5\u8bc6\u522b\u6570\u5b57\uff1f\u300b\uff0c\u5f88\u660e\u663e\u662f\u5173\u4e8e\u6587\u7ae0\u7684\u6587\u7ae0\u54c8\u54c8\u54c8\uff0c\u5176\u4e2d\u5185\u5bb9\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230\u7b49\u7b49\uff0c\u5982\u679c\u80fd\u5e2e\u5230\u4f60\uff0c\u89c9\u5f97\u5f88\u4e0d\u9519\u7684\u8bdd\uff0c\u6b22\u8fce\u5404\u4f4d\u591a\u591a\u70b9\u8bc4\u548c\u5206\u4eab\uff01 antlr \u8bed\u6cd5\u62a5\u9519\u89e3\u6790 \u521a\u63a5\u89e6 antlr \u7684\u5f00\u53d1\u8005\u53ef\u80fd\u4f1a\u9047\u5230\u6700\u7b80\u5355\u7684\u8ba1\u7b97\u8bed\u6cd5\u62a5\u9519\u3002\u672c\u6587\u5c06\u6df1\u5165\u63a2\u7a76\u4e00\u4e2a\u7b80\u5355\u7684 antlr \u8bed\u6cd5\u793a\u4f8b\uff0c\u5e76\u89e3\u51b3\u9047\u5230\u7684\u8bed\u6cd5\u9519\u8bef\u3002 \u9519\u8bef\u4fe1\u606f \u9884\u5904\u7406\u7684 antlr \u8bed\u6cd5\u5982\u4e0b\uff1a grammar expr; prog: expr eof; expr: expr(&#8216;*&#8217;|&#8217;\/&#8217;) expr #multiordiv | expr(&#8216;+&#8217;|&#8217;-&#8216;) expr #addorsub | int #lieteral | &#8216;(&#8216; expr &#8216;)&#8217; #single ; newline : [\\r\\n]+ -&amp;gt; skip; \u8fd0\u884c\u540e\uff0c\u663e\u793a\u4ee5\u4e0b\u9519\u8bef\u6d88\u606f\uff1a 1:1 token recognition error at: &#8216;+&#8217; 1:3 token recognition error at: &#8216;*&#8217; 1:2 mismatched [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-49339","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49339","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=49339"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49339\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=49339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=49339"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=49339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}