\u89e3\u6790\u6574\u6570<\/strong><\/span><\/div>\n\u8ba9\u6211\u4eec\u6765\u89e3\u6790\u4e00\u4e9b\u6570\u5b57\u3002\u8fd9\u770b\u8d77\u6765\u4f1a\u5f88\u7b80\u5355\uff0c\u53ea\u9700\u5728\u4e00\u4e2a\u6587\u672c\u4e32\u4e2d\u5bfb\u627e\u6bd7\u90bb\u7684\u6570\u5b57\uff0c\u4f46\u662f\u8ba9\u6211\u4eec\u5c1d\u8bd5\u53bb\u5904\u7406\u6240\u6709\u5f62\u5f0f\u7684\u6570\u5b57\uff1a\u6574\u6570\u548c\u6d6e\u70b9\u6570\u3001\u5341\u516d\u8fdb\u5236\u6570\u548c\u516b\u8fdb\u5236\u6570\u3001\u79d1\u5b66\u8ba1\u6570\u3001\u8d1f\u6570\u3002\u89e3\u6790\u6570\u5b57\u5f88\u7b80\u5355\uff0c\u6b63\u786e\u89e3\u6790\u5374\u5f88\u96be\u3002
\n\u4eb2\u81ea\u6784\u5efa\u8fd9\u4e2a\u4ee3\u7801\u5c06\u4f1a\u5f88\u56f0\u96be\uff0c\u4f1a\u6709\u5f88\u591a\u95ee\u9898\uff0c\u4f1a\u4f34\u968f\u6709\u8bb8\u591a\u7279\u6b8a\u7684\u60c5\u51b5\uff0c\u6bd4\u5982\u6709\u65f6\u4f1a\u76f8\u4e92\u77db\u76fe\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u6216\u8bb8\u53ef\u4ee5\u505a\u7684\u8fd9\u4e00\u70b9\uff0c\u4f46\u662f\u5b83\u4f1a\u975e\u5e38\u4e11\u964b\u800c\u96be\u4ee5\u7ef4\u62a4\u3002\u8ba9\u6211\u4eec\u7528 Ohm \u6765\u8bd5\u8bd5\u3002
\n\u7528 Ohm \u6784\u5efa\u7684\u89e3\u6790\u5668\u6d89\u53ca\u4e09\u4e2a\u90e8\u5206\uff1a\u8bed\u6cd5\uff08grammar\uff09\u3001\u8bed\u4e49\uff08semantics\uff09\u548c\u6d4b\u8bd5\uff08tests\uff09\u3002\u6211\u901a\u5e38\u6311\u9009\u95ee\u9898\u7684\u4e00\u90e8\u5206\u4e3a\u5b83\u5199\u6d4b\u8bd5\uff0c\u7136\u540e\u6784\u5efa\u8db3\u591f\u7684\u8bed\u6cd5\u548c\u8bed\u4e49\u6765\u4f7f\u6d4b\u8bd5\u901a\u8fc7\u3002\u7136\u540e\u6211\u518d\u6311\u9009\u95ee\u9898\u7684\u53e6\u4e00\u90e8\u5206\uff0c\u589e\u52a0\u66f4\u591a\u7684\u6d4b\u8bd5\u3001\u66f4\u65b0\u8bed\u6cd5\u548c\u8bed\u4e49\uff0c\u4ece\u800c\u786e\u4fdd\u6240\u6709\u7684\u6d4b\u8bd5\u80fd\u591f\u7ee7\u7eed\u901a\u8fc7\u3002\u5373\u4f7f\u6211\u4eec\u6709\u4e86\u65b0\u7684\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5199\u89e3\u6790\u5668\u4ece\u6982\u5ff5\u4e0a\u6765\u8bf4\u4f9d\u65e7\u5f88\u590d\u6742\u3002\u6d4b\u8bd5\u662f\u7528\u4e00\u79cd\u5408\u7406\u7684\u65b9\u5f0f\u6765\u6784\u5efa\u89e3\u6790\u5668\u7684\u552f\u4e00\u65b9\u6cd5\u3002\u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u5f00\u59cb\u5de5\u4f5c\u3002
\n\u6211\u4eec\u5c06\u4ece\u6574\u6570\u5f00\u59cb\u3002\u4e00\u4e2a\u6574\u6570\u7531\u4e00\u7cfb\u5217\u76f8\u4e92\u6bd7\u90bb\u7684\u6570\u5b57\u7ec4\u6210\u3002\u8ba9\u6211\u4eec\u628a\u4e0b\u9762\u7684\u5185\u5bb9\u653e\u5165\u4e00\u4e2a\u53eb\u505a grammar.ohm \u7684\u6587\u4ef6\u4e2d\uff1a<\/p>\n
CoolNums {\r\n \/\/ just a basic integer\r\n Number = digit+\r\n}\r\n<\/pre>\n\u8fd9\u521b\u9020\u4e86\u4e00\u6761\u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u6570\u5b57digit<\/span>\u53eb\u4f5c Number<\/span> \u7684\u5355\u4e00\u89c4\u5219\u3002+<\/span>\u610f\u5473\u7740\u4e00\u4e2a\u6216\u66f4\u591a\uff0c\u5c31\u5728\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u4e00\u6837\u3002\u5f53\u6709\u4e00\u4e2a\u6216\u66f4\u591a\u7684\u6570\u5b57\u65f6\uff0c\u8fd9\u4e2a\u89c4\u5219\u5c06\u4f1a\u5339\u914d\u5b83\u4eec\uff0c\u5982\u679c\u6ca1\u6709\u6570\u5b57\u6216\u8005\u6709\u4e00\u4e9b\u4e0d\u662f\u6570\u5b57\u7684\u4e1c\u897f\u5c06\u4e0d\u4f1a\u5339\u914d\u3002\u201c\u6570\u5b57\uff08digit<\/span>\uff09\u201d\u7684\u5b9a\u4e49\u662f\u4ece 0 \u5230 9 \u5176\u4e2d\u7684\u4e00\u4e2a\u5b57\u7b26\u3002digit<\/span> \u4e5f\u662f\u50cf Number<\/span>\u4e00\u6837\u7684\u89c4\u5219\uff0c\u4f46\u662f\u5b83\u662f Ohm \u7684\u5176\u4e2d\u4e00\u6761\u6784\u5efa\u89c4\u5219\u56e0\u6b64\u6211\u4eec\u4e0d\u9700\u8981\u53bb\u5b9a\u4e49\u5b83\u3002\u5982\u679c\u6211\u4eec\u60f3\u7684\u8bdd\u53ef\u4ee5\u63a8\u7ffb\u5b83\uff0c\u4f46\u5728\u8fd9\u65f6\u5019\u8fd9\u6ca1\u6709\u4efb\u4f55\u610f\u4e49\uff0c\u6bd5\u7adf\u6211\u4eec\u4e0d\u6253\u7b97\u53bb\u53d1\u660e\u4e00\u79cd\u65b0\u7684\u6570\u3002
\n\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u8bfb\u5165\u8fd9\u4e2a\u8bed\u6cd5\u5e76\u7528 Ohm \u5e93\u6765\u8fd0\u884c\u5b83\u3002
\n\u628a\u5b83\u653e\u5165 test1.js\uff1a<\/p>\nvar ohm = require('ohm-js');\r\nvar fs = require('fs');\r\nvar assert = require('assert');\r\nvar grammar = ohm.grammar(fs.readFileSync('src\/blog_numbers\/syntax1.ohm').toString());\r\n<\/pre>\nOhm.grammar<\/span>\u8c03\u7528\u5c06\u8bfb\u5165\u8be5\u6587\u4ef6\u5e76\u89e3\u6790\u6210\u4e00\u4e2a\u8bed\u6cd5\u5bf9\u8c61\u3002\u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u589e\u52a0\u4e00\u4e9b\u8bed\u4e49\u3002\u628a\u4e0b\u9762\u5185\u5bb9\u589e\u52a0\u5230\u4f60\u7684 JavaScript \u6587\u4ef6\u4e2d\uff1a<\/p>\nvar sem = grammar.createSemantics().addOperation('toJS', {\r\n Number: function(a) {\r\n return parseInt(this.sourceString,10);\r\n }\r\n});\r\n<\/pre>\n\u8fd9\u901a\u8fc7 toJS<\/span>\u64cd\u4f5c\u521b\u9020\u4e86\u4e00\u4e2a\u53eb\u4f5c sem<\/span>\u7684\u8bed\u6cd5\u96c6\u3002\u8fd9\u4e9b\u8bed\u4e49\u672c\u8d28\u4e0a\u662f\u4e00\u4e9b\u5bf9\u5e94\u5230\u8bed\u6cd5\u4e2d\u6bcf\u4e2a\u89c4\u5219\u7684\u51fd\u6570\u3002\u6bcf\u4e2a\u51fd\u6570\u5f53\u4e0e\u4e4b\u76f8\u5339\u914d\u7684\u8bed\u6cd5\u89c4\u5219\u88ab\u89e3\u6790\u65f6\u5c06\u4f1a\u88ab\u8c03\u7528\u3002\u4e0a\u9762\u7684 Number<\/span> \u51fd\u6570\u5c06\u4f1a\u5728\u8bed\u6cd5\u4e2d\u7684 Number<\/span>\u89c4\u5219\u88ab\u89e3\u6790\u65f6\u88ab\u8c03\u7528\u3002\u8bed\u6cd5\uff08grammar\uff09\u5b9a\u4e49\u4e86\u5728\u8bed\u8a00\u4e2d\u8fd9\u4e9b\u4ee3\u7801\u662f\u4ec0\u4e48\uff0c\u8bed\u4e49\uff08semantics\uff09\u5b9a\u4e49\u4e86\u5f53\u8fd9\u4e9b\u4ee3\u7801\u88ab\u89e3\u6790\u65f6\u5e94\u8be5\u505a\u4ec0\u4e48\u3002
\n\u8bed\u4e49\u51fd\u6570\u80fd\u591f\u505a\u6211\u4eec\u60f3\u505a\u7684\u4efb\u4f55\u4e8b\uff0c\u6bd4\u5982\u6253\u5370\u51fa\u6545\u969c\u4fe1\u606f\u3001\u521b\u5efa\u5bf9\u8c61\uff0c\u6216\u8005\u5728\u4efb\u4f55\u5b50\u8282\u70b9\u4e0a\u9012\u5f52\u8c03\u7528 toJS<\/span>\u3002\u6b64\u65f6\u6211\u4eec\u4ec5\u4ec5\u60f3\u628a\u5339\u914d\u7684\u6587\u672c\u8f6c\u6362\u6210\u771f\u6b63\u7684 JavaScript \u6574\u6570\u3002
\n\u6240\u6709\u7684\u8bed\u4e49\u51fd\u6570\u6709\u4e00\u4e2a\u5185\u542b\u7684 this<\/span>\u5bf9\u8c61\uff0c\u5e26\u6709\u4e00\u4e9b\u6709\u7528\u7684\u5c5e\u6027\u3002\u5176 source<\/span> \u5c5e\u6027\u4ee3\u8868\u4e86\u8f93\u5165\u6587\u672c\u4e2d\u548c\u8fd9\u4e2a\u8282\u70b9\u76f8\u5339\u914d\u7684\u90e8\u5206\u3002this.sourceString<\/span>\u662f\u4e00\u4e2a\u5339\u914d\u8f93\u5165\u7684\u4e32\uff0c\u8c03\u7528\u5185\u7f6e\u5728 JavaScript \u4e2d\u7684parseInt<\/span>\u51fd\u6570\u4f1a\u628a\u8fd9\u4e2a\u4e32\u8f6c\u6362\u6210\u4e00\u4e2a\u6570\u3002\u4f20\u7ed9 parseInt<\/span>\u7684 10<\/span>\u8fd9\u4e2a\u53c2\u6570\u544a\u8bc9 JavaScript \u6211\u4eec\u8f93\u5165\u7684\u662f\u4e00\u4e2a\u4ee5 10<\/span>\u4e3a\u57fa\u5e95\uff0810 \u8fdb\u5236\uff09\u7684\u6570\u3002\u5982\u679c\u5c11\u4e86\u8fd9\u4e2a\u53c2\u6570\uff0c JavaScript \u4e5f\u4f1a\u5047\u5b9a\u4ee5 10 \u4e3a\u57fa\u5e95\uff0c\u4f46\u662f\u6211\u4eec\u628a\u5b83\u5305\u542b\u5728\u91cc\u9762\u56e0\u4e3a\u540e\u9762\u6211\u4eec\u5c06\u652f\u6301\u4ee5 16 \u4e3a\u57fa\u5e95\u7684\u6570\uff0c\u6240\u4ee5\u4f7f\u4e4b\u660e\u786e\u6bd4\u8f83\u597d\u3002
\n\u65e2\u7136\u6211\u4eec\u6709\u4e00\u4e9b\u8bed\u6cd5\uff0c\u8ba9\u6211\u4eec\u6765\u5b9e\u9645\u89e3\u6790\u4e00\u4e9b\u4e1c\u897f\u770b\u4e00\u770b\u6211\u4eec\u7684\u89e3\u6790\u5668\u662f\u5426\u80fd\u591f\u5de5\u4f5c\u3002\u5982\u4f55\u77e5\u9053\u6211\u4eec\u7684\u89e3\u6790\u5668\u53ef\u4ee5\u5de5\u4f5c\uff1f\u901a\u8fc7\u6d4b\u8bd5\uff0c\u8bb8\u591a\u8bb8\u591a\u7684\u6d4b\u8bd5\uff0c\u6bcf\u4e00\u4e2a\u53ef\u80fd\u7684\u8fb9\u7f18\u60c5\u51b5\u90fd\u9700\u8981\u4e00\u4e2a\u6d4b\u8bd5\u3002
\n\u4f7f\u7528\u6807\u51c6\u7684\u65ad\u8a00 assert<\/span>API\uff0c\u4ee5\u4e0b\u8fd9\u4e2a\u6d4b\u8bd5\u51fd\u6570\u80fd\u591f\u5339\u914d\u4e00\u4e9b\u8f93\u5165\u5e76\u8fd0\u7528\u6211\u4eec\u7684\u8bed\u4e49\u628a\u5b83\u8f6c\u6362\u6210\u4e00\u4e2a\u6570\uff0c\u7136\u540e\u628a\u8fd9\u4e2a\u6570\u548c\u6211\u4eec\u671f\u671b\u7684\u8f93\u5165\u8fdb\u884c\u6bd4\u8f83\u3002<\/p>\n function test(input, answer) {\r\n var match = grammar.match(input);\r\n if(match.failed()) return console.log(\"input failed to match \" + input + match.message); \r\n var result = sem(match).toJS();\r\n assert.deepEqual(result,answer);\r\n console.log('success = ', result, answer);\r\n }\r\n<\/pre>\n\u5c31\u662f\u5982\u6b64\u3002\u73b0\u5728\u6211\u4eec\u80fd\u591f\u4e3a\u5404\u79cd\u4e0d\u540c\u7684\u6570\u5199\u4e00\u5806\u6d4b\u8bd5\u3002\u5982\u679c\u5339\u914d\u5931\u8d25\u6211\u4eec\u7684\u811a\u672c\u5c06\u4f1a\u629b\u51fa\u4e00\u4e2a\u4f8b\u5916\u3002\u5426\u5219\u5c31\u6253\u5370\u6210\u529f\u4fe1\u606f\u3002\u8ba9\u6211\u4eec\u5c1d\u8bd5\u4e00\u4e0b\uff0c\u628a\u4e0b\u9762\u8fd9\u4e9b\u5185\u5bb9\u52a0\u5165\u5230\u811a\u672c\u4e2d\uff1a<\/p>\n
test(\"123\",123);\r\n test(\"999\",999);\r\n test(\"abc\",999);\r\n<\/pre>\n\u7136\u540e\u7528node test1.js<\/span>\u8fd0\u884c\u811a\u672c\u3002
\n\u4f60\u7684\u8f93\u51fa\u5e94\u8be5\u662f\u8fd9\u6837\uff1a<\/p>\nsuccess = 123 123\r\nsuccess = 999 999\r\ninput failed to match abcLine 1, col 1:\r\n> 1 | abc\r\n ^\r\nExpected a digit\r\n<\/pre>\n\u771f\u9177\u3002\u6b63\u5982\u9884\u671f\u7684\u90a3\u6837\uff0c\u524d\u4e24\u4e2a\u6210\u529f\u4e86\uff0c\u7b2c\u4e09\u4e2a\u5931\u8d25\u4e86\u3002\u66f4\u597d\u7684\u662f\uff0cOhm \u81ea\u52a8\u7ed9\u4e86\u6211\u4eec\u4e00\u4e2a\u5f88\u68d2\u7684\u9519\u8bef\u4fe1\u606f\u6307\u51fa\u5339\u914d\u5931\u8d25\u3002<\/p>\n
\u6d6e\u70b9\u6570<\/strong><\/span><\/div>\n\u6211\u4eec\u7684\u89e3\u6790\u5668\u5de5\u4f5c\u4e86\uff0c\u4f46\u662f\u5b83\u505a\u7684\u5de5\u4f5c\u4e0d\u662f\u5f88\u6709\u8da3\u3002\u8ba9\u6211\u4eec\u628a\u5b83\u6269\u5c55\u6210\u65e2\u80fd\u89e3\u6790\u6574\u6570\u53c8\u80fd\u89e3\u6790\u6d6e\u70b9\u6570\u3002\u6539\u53d8 grammar.ohm \u6587\u4ef6\u4f7f\u5b83\u770b\u8d77\u6765\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n
CoolNums {\r\n \/\/ just a basic integer\r\n Number = float | int\r\n int = digit+\r\n float = digit+ \".\" digit+\r\n}\r\n<\/pre>\n\u8fd9\u628aNumber<\/span>\u89c4\u5219\u6539\u53d8\u6210\u6307\u5411\u4e00\u4e2a\u6d6e\u70b9\u6570\uff08float<\/span>\uff09\u6216\u8005\u4e00\u4e2a\u6574\u6570\uff08int<\/span>\uff09\u3002\u8fd9\u4e2a |<\/span> \u4ee3\u8868\u7740\u201c\u6216\u201d\u3002\u6211\u4eec\u628a\u8fd9\u4e2a\u8bfb\u6210\u201c\u4e00\u4e2a Number<\/span>\u7531\u4e00\u4e2a\u6d6e\u70b9\u6570\u6216\u8005\u4e00\u4e2a\u6574\u6570\u6784\u6210\u3002\u201d\u7136\u540e\u6574\u6570\uff08int<\/span>\uff09\u5b9a\u4e49\u6210 digit+<\/span>\uff0c\u6d6e\u70b9\u6570\uff08float<\/span>\uff09\u5b9a\u4e49\u6210digit+<\/span>\u540e\u9762\u8ddf\u7740\u4e00\u4e2a\u53e5\u53f7\u7136\u540e\u518d\u8ddf\u7740\u53e6\u4e00\u4e2adigit+<\/span>\u3002\u8fd9\u610f\u5473\u7740\u5728\u53e5\u53f7\u524d\u548c\u53e5\u53f7\u540e\u90fd\u81f3\u5c11\u8981\u6709\u4e00\u4e2a\u6570\u5b57\u3002\u5982\u679c\u4e00\u4e2a\u6570\u4e2d\u6ca1\u6709\u4e00\u4e2a\u53e5\u53f7\u90a3\u4e48\u5b83\u5c31\u4e0d\u662f\u4e00\u4e2a\u6d6e\u70b9\u6570\uff0c\u56e0\u6b64\u5c31\u662f\u4e00\u4e2a\u6574\u6570\u3002
\n\u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u518d\u6b21\u770b\u4e00\u4e0b\u6211\u4eec\u7684\u8bed\u4e49\u529f\u80fd\u3002\u7531\u4e8e\u6211\u4eec\u73b0\u5728\u6709\u4e86\u65b0\u7684\u89c4\u5219\u6240\u4ee5\u6211\u4eec\u9700\u8981\u65b0\u7684\u529f\u80fd\u51fd\u6570\uff1a\u4e00\u4e2a\u4f5c\u4e3a\u6574\u6570\u7684\uff0c\u4e00\u4e2a\u4f5c\u4e3a\u6d6e\u70b9\u6570\u7684\u3002<\/p>\nvar sem = grammar.createSemantics().addOperation('toJS', {\r\n Number: function(a) {\r\n return a.toJS();\r\n },\r\n int: function(a) {\r\n console.log(\"doing int\", this.sourceString);\r\n return parseInt(this.sourceString,10);\r\n },\r\n float: function(a,b,c) {\r\n console.log(\"doing float\", this.sourceString);\r\n return parseFloat(this.sourceString);\r\n }\r\n});\r\n<\/pre>\n\u8fd9\u91cc\u6709\u4e24\u4ef6\u4e8b\u60c5\u9700\u8981\u6ce8\u610f\u3002\u9996\u5148\uff0c\u6574\u6570\uff08int<\/span>\uff09\u3001\u6d6e\u70b9\u6570\uff08float<\/span>\uff09\u548c\u6570\uff08Number<\/span>\uff09\u90fd\u6709\u76f8\u5339\u914d\u7684\u8bed\u6cd5\u89c4\u5219\u548c\u51fd\u6570\u3002\u7136\u800c\uff0c\u9488\u5bf9Number<\/span>\u7684\u529f\u80fd\u4e0d\u518d\u6709\u4efb\u4f55\u610f\u4e49\u3002\u5b83\u63a5\u6536\u5b50\u8282\u70b9 a<\/span> \u7136\u540e\u8fd4\u56de\u8be5\u5b50\u8282\u70b9\u7684 toJS<\/span> \u7ed3\u679c\u3002\u6362\u53e5\u8bdd\u8bf4\uff0cNumber<\/span> \u89c4\u5219\u7b80\u5355\u7684\u8fd4\u56de\u76f8\u5339\u914d\u7684\u5b50\u89c4\u5219\u3002\u7531\u4e8e\u8fd9\u662f\u5728 Ohm \u4e2d\u4efb\u4f55\u89c4\u5219\u7684\u9ed8\u8ba4\u884c\u4e3a\uff0c\u56e0\u6b64\u5b9e\u9645\u4e0a\u6211\u4eec\u4e0d\u7528\u53bb\u8003\u8651 Number<\/span> \u7684\u4f5c\u7528\uff0cOhm \u4f1a\u66ff\u6211\u4eec\u505a\u597d\u8fd9\u4ef6\u4e8b\u3002
\n\u5176\u6b21\uff0c\u6574\u6570\uff08int<\/span>\uff09\u6709\u4e00\u4e2a\u53c2\u6570 a<\/span>\uff0c\u7136\u800c\u6d6e\u70b9\u6570\u6709\u4e09\u4e2a\uff1aa\u3001b\u548cc<\/span>\u3002\u8fd9\u662f\u7531\u4e8e\u89c4\u5219\u7684\u5b9e\u53c2\u6570\u91cf\uff08arity\uff09\u51b3\u5b9a\u7684\u3002\u5b9e\u53c2\u6570\u91cf\uff08arity\uff09\u610f\u5473\u7740\u4e00\u4e2a\u89c4\u5219\u91cc\u9762\u6709\u591a\u5c11\u53c2\u6570\u3002\u5982\u679c\u6211\u4eec\u56de\u8fc7\u5934\u53bb\u770b\u8bed\u6cd5\uff0c\u6d6e\u70b9\u6570\uff08float<\/span>\uff09\u7684\u89c4\u5219\u662f\uff1a<\/p>\n float = digit+ \".\" digit+\r\n<\/pre>\n\u6d6e\u70b9\u6570\u89c4\u5219\u901a\u8fc7\u4e09\u4e2a\u90e8\u5206\u6765\u5b9a\u4e49\uff1a\u7b2c\u4e00\u4e2a digit+\u3001.\u3001\u4ee5\u53ca\u7b2c\u4e8c\u4e2a digit+<\/span>\u3002\u8fd9\u4e09\u4e2a\u90e8\u5206\u90fd\u4f1a\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u6d6e\u70b9\u6570\u7684\u529f\u80fd\u51fd\u6570\u3002\u56e0\u6b64\u6d6e\u70b9\u6570\u5fc5\u987b\u6709\u4e09\u4e2a\u53c2\u6570\uff0c\u5426\u5219 Ohm \u5e93\u4f1a\u7ed9\u51fa\u4e00\u4e2a\u9519\u8bef\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u6211\u4eec\u4e0d\u7528\u5728\u610f\u53c2\u6570\uff0c\u56e0\u4e3a\u6211\u4eec\u4ec5\u4ec5\u76f4\u63a5\u652b\u53d6\u4e86\u8f93\u5165\u4e32\uff0c\u4f46\u662f\u6211\u4eec\u4ecd\u7136\u9700\u8981\u53c2\u6570\u5217\u5728\u90a3\u91cc\u6765\u907f\u514d\u7f16\u8bd1\u5668\u9519\u8bef\u3002\u540e\u9762\u6211\u4eec\u5c06\u5b9e\u9645\u4f7f\u7528\u5176\u4e2d\u4e00\u4e9b\u53c2\u6570\u3002
\n\u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u4e3a\u65b0\u7684\u6d6e\u70b9\u6570\u652f\u6301\u6dfb\u52a0\u66f4\u591a\u7684\u6d4b\u8bd5\u3002<\/p>\ntest(\"123\",123);\r\ntest(\"999\",999);\r\n\/\/test(\"abc\",999);\r\ntest('123.456',123.456);\r\ntest('0.123',0.123);\r\ntest('.123',0.123);\r\n<\/pre>\n\u6ce8\u610f\u6700\u540e\u4e00\u4e2a\u6d4b\u8bd5\u5c06\u4f1a\u5931\u8d25\u3002\u4e00\u4e2a\u6d6e\u70b9\u6570\u5fc5\u987b\u4ee5\u4e00\u4e2a\u6570\u5f00\u59cb\uff0c\u5373\u4f7f\u5b83\u5c31\u662f\u4e2a 0\uff0c.123<\/span> \u4e0d\u662f\u6709\u6548\u7684\uff0c\u5b9e\u9645\u4e0a\u771f\u6b63\u7684 JavaScript \u8bed\u8a00\u4e5f\u6709\u76f8\u540c\u7684\u89c4\u5219\u3002<\/p>\n\u5341\u516d\u8fdb\u5236\u6570<\/strong><\/span><\/div>\n\u73b0\u5728\u6211\u4eec\u5df2\u7ecf\u6709\u4e86\u6574\u6570\u548c\u6d6e\u70b9\u6570\uff0c\u4f46\u662f\u8fd8\u6709\u4e00\u4e9b\u5176\u5b83\u7684\u6570\u7684\u8bed\u6cd5\u6700\u597d\u53ef\u4ee5\u652f\u6301\uff1a\u5341\u516d\u8fdb\u5236\u6570\u548c\u79d1\u5b66\u8ba1\u6570\u3002\u5341\u516d\u8fdb\u5236\u6570\u662f\u4ee5 16 \u4e3a\u57fa\u5e95\u7684\u6574\u6570\u3002\u5341\u516d\u8fdb\u5236\u6570\u7684\u6570\u5b57\u80fd\u4ece 0 \u5230 9 \u548c\u4ece A \u5230 F\u3002\u5341\u516d\u8fdb\u5236\u6570\u7ecf\u5e38\u7528\u5728\u8ba1\u7b97\u673a\u79d1\u5b66\u4e2d\uff0c\u5f53\u7528\u4e8c\u8fdb\u5236\u6570\u636e\u5de5\u4f5c\u65f6\uff0c\u4f60\u53ef\u4ee5\u4ec5\u4ec5\u4f7f\u7528\u4e24\u4e2a\u6570\u5b57\u8868\u793a 0 \u5230 255 \u7684\u6570\u3002
\n\u5728\u7edd\u5927\u591a\u6570\u6e90\u81ea C \u7684\u7f16\u7a0b\u8bed\u8a00\uff08\u5305\u62ec JavaScript\uff09\uff0c\u5341\u516d\u8fdb\u5236\u6570\u901a\u8fc7\u5728\u524d\u9762\u52a0\u4e0a 0x<\/span> \u6765\u5411\u7f16\u8bd1\u5668\u8868\u660e\u540e\u9762\u8ddf\u7684\u662f\u4e00\u4e2a\u5341\u516d\u8fdb\u5236\u6570\u3002\u4e3a\u4e86\u8ba9\u6211\u4eec\u7684\u89e3\u6790\u5668\u652f\u6301\u5341\u516d\u8fdb\u5236\u6570\uff0c\u6211\u4eec\u53ea\u9700\u8981\u6dfb\u52a0\u53e6\u4e00\u6761\u89c4\u5219\u3002<\/p>\n Number = hex | float | int\r\n int = digit+\r\n float = digit+ \".\" digit+\r\n hex = \"0x\" hexDigit+\r\n hexDigit := \"0\"..\"9\" | \"a\"..\"f\" | \"A\"..\"F\"\r\n<\/pre>\n\u6211\u5b9e\u9645\u4e0a\u5df2\u7ecf\u589e\u52a0\u4e86\u4e24\u6761\u89c4\u5219\u3002\u5341\u516d\u8fdb\u5236\u6570\uff08hex<\/span>\uff09\u8868\u660e\u5b83\u662f\u4e00\u4e2a 0x<\/span> \u540e\u9762\u4e00\u4e2a\u6216\u591a\u4e2a\u5341\u516d\u8fdb\u5236\u6570\u5b57\uff08hexDigits<\/span>\uff09\u7684\u4e32\u3002\u4e00\u4e2a\u5341\u516d\u8fdb\u5236\u6570\u5b57\uff08hexDigit<\/span>\uff09\u662f\u4ece 0 \u5230 9\uff0c\u6216\u4ece a \u5230 f\uff0c\u6216 A \u5230 F\uff08\u5305\u62ec\u5927\u5199\u548c\u5c0f\u5199\u7684\u60c5\u51b5\uff09\u7684\u4e00\u4e2a\u5b57\u7b26\u3002\u6211\u4e5f\u4fee\u6539\u4e86 Number<\/span> \u89c4\u5219\u6765\u8bc6\u522b\u5341\u516d\u8fdb\u5236\u6570\u4f5c\u4e3a\u53e6\u5916\u4e00\u79cd\u53ef\u80fd\u7684\u60c5\u51b5\u3002\u73b0\u5728\u6211\u4eec\u53ea\u9700\u8981\u53e6\u4e00\u6761\u9488\u5bf9\u5341\u516d\u8fdb\u5236\u6570\u7684\u529f\u80fd\u89c4\u5219\u3002<\/p>\n hex: function(a,b) {\r\n return parseInt(this.sourceString,16);\r\n }\r\n<\/pre>\n\u6ce8\u610f\u5230\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u628a16<\/span> \u4f5c\u4e3a\u57fa\u5e95\u4f20\u9012\u7ed9parseInt<\/span>\uff0c\u56e0\u4e3a\u6211\u4eec\u5e0c\u671b JavaScript \u77e5\u9053\u8fd9\u662f\u4e00\u4e2a\u5341\u516d\u8fdb\u5236\u6570\u3002
\n\u6211\u7565\u8fc7\u4e86\u4e00\u4e9b\u5f88\u91cd\u8981\u9700\u8981\u6ce8\u610f\u7684\u4e8b\u3002hexDigit<\/span> \u7684\u89c4\u5219\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n hexDigit := \"0\"..\"9\" | \"a\"..\"f\" | \"A\"..\"F\"\r\n<\/pre>\n\u6ce8\u610f\u6211\u4f7f\u7528\u7684\u662f:=<\/span>\u800c\u4e0d\u662f =<\/span>\u3002\u5728 Ohm \u4e2d\uff0c:=<\/span>\u662f\u5f53\u4f60\u9700\u8981\u63a8\u7ffb\u4e00\u6761\u89c4\u5219\u7684\u65f6\u5019\u4f7f\u7528\u3002\u8fd9\u8868\u660e Ohm \u5df2\u7ecf\u6709\u4e86\u4e00\u6761\u9488\u5bf9 hexDigit<\/span> \u7684\u9ed8\u8ba4\u89c4\u5219\uff0c\u5c31\u50cf digit<\/span>\u3001space<\/span> \u7b49\u4e00\u5806\u5176\u4ed6\u7684\u4e1c\u897f\u3002\u5982\u679c\u6211\u4f7f\u7528\u4e86 =<\/span>, Ohm \u5c06\u4f1a\u62a5\u544a\u4e00\u4e2a\u9519\u8bef\u3002\u8fd9\u662f\u4e00\u4e2a\u68c0\u67e5\uff0c\u4ece\u800c\u907f\u514d\u6211\u65e0\u610f\u8bc6\u7684\u63a8\u7ffb\u4e00\u4e2a\u89c4\u5219\u3002\u7531\u4e8e\u65b0\u7684hexDigit<\/span> \u89c4\u5219\u548c Ohm \u7684\u6784\u5efa\u89c4\u5219\u4e00\u6837\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u628a\u5b83\u6ce8\u91ca\u6389\uff0c\u7136\u540e\u8ba9 Ohm \u81ea\u5df1\u6765\u5b9e\u73b0\u5b83\u3002\u6211\u7559\u4e0b\u8fd9\u4e2a\u89c4\u5219\u53ea\u662f\u56e0\u4e3a\u8fd9\u6837\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5b83\u5b9e\u9645\u4e0a\u662f\u5982\u4f55\u8fdb\u884c\u7684\u3002
\n\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u6dfb\u52a0\u66f4\u591a\u7684\u6d4b\u8bd5\u7136\u540e\u770b\u5230\u5341\u516d\u8fdb\u5236\u6570\u771f\u7684\u80fd\u5de5\u4f5c\uff1a<\/p>\ntest('0x456',0x456);\r\ntest('0xFF',255);\r\n<\/pre>\n\u79d1\u5b66\u8ba1\u6570<\/strong><\/span><\/div>\n\u6700\u540e\uff0c\u8ba9\u6211\u4eec\u6765\u652f\u6301\u79d1\u5b66\u8ba1\u6570\u3002\u79d1\u5b66\u8ba1\u6570\u662f\u9488\u5bf9\u975e\u5e38\u5927\u6216\u975e\u5e38\u5c0f\u7684\u6570\u7684\uff0c\u6bd4\u5982 1.8\u00d710^3<\/span>\u3002\u5728\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u79d1\u5b66\u8ba1\u6570\u6cd5\u8868\u793a\u7684\u6570\u4f1a\u5199\u6210\u8fd9\u6837\uff1a1.8e3 \u8868\u793a 18000\uff0c\u6216\u8005 1.8e-3 \u8868\u793a .018\u3002\u8ba9\u6211\u4eec\u589e\u52a0\u53e6\u5916\u4e00\u5bf9\u89c4\u5219\u6765\u652f\u6301\u8fd9\u4e2a\u6307\u6570\u8868\u793a\uff1a<\/p>\n float = digit+ \".\" digit+ exp?\r\n exp = \"e\" \"-\"? digit+\r\n<\/pre>\n\u4e0a\u9762\u5728\u6d6e\u70b9\u6570\u89c4\u5219\u672b\u5c3e\u589e\u52a0\u4e86\u4e00\u4e2a\u6307\u6570\uff08exp<\/span>\uff09\u89c4\u5219\u548c\u4e00\u4e2a?<\/span>\u3002?<\/span> \u8868\u793a\u6ca1\u6709\u6216\u6709\u4e00\u4e2a\uff0c\u6240\u4ee5\u6307\u6570\uff08exp<\/span>\uff09\u662f\u53ef\u9009\u7684\uff0c\u4f46\u662f\u4e0d\u80fd\u8d85\u8fc7\u4e00\u4e2a\u3002\u589e\u52a0\u6307\u6570\uff08exp<\/span>\uff09\u89c4\u5219\u4e5f\u6539\u53d8\u4e86\u6d6e\u70b9\u6570\u89c4\u5219\u7684\u5b9e\u53c2\u6570\u91cf\uff0c\u6240\u4ee5\u6211\u4eec\u9700\u8981\u4e3a\u6d6e\u70b9\u6570\u529f\u80fd\u589e\u52a0\u53e6\u4e00\u4e2a\u53c2\u6570\uff0c\u5373\u4f7f\u6211\u4eec\u4e0d\u4f7f\u7528\u5b83\u3002<\/p>\n float: function(a,b,c,d) {\r\n console.log(\"doing float\", this.sourceString);\r\n return parseFloat(this.sourceString);\r\n },\r\n<\/pre>\n\u73b0\u5728\u6211\u4eec\u7684\u6d4b\u8bd5\u53ef\u4ee5\u901a\u8fc7\u4e86\uff1a<\/p>\n
test('4.8e10',4.8e10);\r\ntest('4.8e-10',4.8e-10);\r\n<\/pre>\n