{"id":207856,"date":"2025-07-08T11:54:03","date_gmt":"2025-07-08T03:54:03","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207856\/"},"modified":"2025-07-08T11:54:03","modified_gmt":"2025-07-08T03:54:03","slug":"antlr-%e9%87%8d%e6%96%b0%e5%a3%b0%e6%98%8e%e4%b8%ba%e5%af%bc%e5%85%a5%e7%9a%84%e5%8c%85%e5%90%8d%e7%a7%b0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207856\/","title":{"rendered":"antlr \u91cd\u65b0\u58f0\u660e\u4e3a\u5bfc\u5165\u7684\u5305\u540d\u79f0"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>antlr \u91cd\u65b0\u58f0\u660e\u4e3a\u5bfc\u5165\u7684\u5305\u540d\u79f0<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-26 09:27:37<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300aantlr \u91cd\u65b0\u58f0\u660e\u4e3a\u5bfc\u5165\u7684\u5305\u540d\u79f0\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6784\u5efa\u4e86\u4e00\u4e2a\u76ee\u5f55 src\uff0c\u5176\u4e2d\u5305\u542b\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<pre>src\/\n  antlr\/\n   basic_levex.go\n   basic_parse.go\n   basicparser_base_visitor.go\n   basicparser_visitor.go\n  example1.go<\/pre>\n<p>antlr \u6587\u4ef6\u770b\u8d77\u6765\u6b63\u786e\uff0c\u5e76\u6784\u5efa\u4e86\u540d\u4e3a basiclexer.g4 \u548c basicparser.g4 \u7684\u6587\u4ef6\u3002<\/p>\n<p>\u6211\u7684 example1.go \u6587\u4ef6\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>package main\n\n\/\/ example1.go\n\nimport (\n    \".\/antlr\"\n    \"fmt\"\n    \"github.com\/antlr\/antlr4\/runtime\/go\/antlr\"\n)\n\nfunc main() {\n    \/\/ setup the input\n    is := antlr.newinputstream(\"1 + 2 * 3\")\n\n    \/\/ create the lexer\n    lexer := antlr.newbasiclexer(is)\n\n    \/\/ read all tokens\n    for {\n        t := lexer.nexttoken()\n        if t.gettokentype() == antlr.tokeneof {\n            break\n        }\n        fmt.printf(\"%s (%q)\\n\",\n            lexer.symbolicnames[t.gettokentype()], t.gettext())\n    }\n}<\/pre>\n<p>\u6211\u5728\u7f16\u8bd1\u65f6\u9047\u5230\u7684\u9519\u8bef\u662f<\/p>\n<pre># command-line-arguments\n.\/example1.go:8:2: antlr redeclared as imported package name\n    previous declaration at .\/example1.go:6:2\n.\/example1.go:16:11: undefined: \"github.com\/antlr\/antlr4\/runtime\/Go\/antlr\".NewBasicLexer<\/pre>\n<p>\u6211\u771f\u7684\u4e0d\u77e5\u9053\u51fa\u4e86\u4ec0\u4e48\u95ee\u9898\u3002\u5982\u4f55\u4fee\u590d\u5b83\u4ee5\u4fbf\u5c06\u6765\u4e0d\u518d\u53d1\u751f\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u8ba9\u6211\u4eec\u4f9d\u6b21\u770b\u770b\u60a8\u7684\u4e24\u4e2a\u95ee\u9898<\/p>\n<h2>\u91cd\u590d\u5bfc\u5165<\/h2>\n<pre>.\/example1.go:8:2: antlr redeclared as imported package name\n    previous declaration at .\/example1.go:6:2<\/pre>\n<p>\u9519\u8bef\u6d88\u606f\u5f88\u597d\u5730\u89e3\u91ca\u4e86\u53d1\u751f\u7684\u60c5\u51b5\uff0c\u5305\u62ec\u4ee5\u4e0b\u884c\uff1a \u5728\u7b2c 8 \u884c\uff0c\u60a8\u5c1d\u8bd5\u5bfc\u5165\u4e0e\u7b2c 6 \u884c\u5df2\u5bfc\u5165\u7684\u540d\u79f0\u76f8\u540c\u7684\u5185\u5bb9\u3002 \u9664\u6b64\u4e4b\u5916\uff0c\uff0c\u5c31\u50cf\u5728\u8bc4\u8bba\u4e2d\u6307\u51fa\u7684\u90a3\u6837\u3002 \u8981\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u8bf7\u5220\u9664\u7b2c 6 \u884c\u4e0a\u7684\u76f8\u5bf9\u5bfc\u5165\u3002<\/p>\n<h2>\u672a\u5b9a\u4e49\u7684\u51fd\u6570<\/h2>\n<pre>.\/example1.go:16:11: undefined: \"github.com\/antlr\/antlr4\/runtime\/Go\/antlr\".NewBasicLexer<\/pre>\n<p>\u60a8\u5c1d\u8bd5\u4f7f\u7528\u7684\u529f\u80fd\u4e0d\u5b58\u5728\u3002<\/p>\n<p>\u5728\u672c\u4f8b\u4e2d\uff0c\u5b83\u5b9e\u9645\u4e0a\u79f0\u4e3a <code>newbaselexer<\/code>\uff0c\u800c\u4e0d\u662f <code>newbasiclexer<\/code>\uff0c\u5982\u60a8\u5728 \u4e2d\u770b\u5230\u7684\u90a3\u6837<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aantlr \u91cd\u65b0\u58f0\u660e\u4e3a\u5bfc\u5165\u7684\u5305\u540d\u79f0\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591aGolang\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207856","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207856","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207856"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207856\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207856"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207856"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207856"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}