{"id":3620,"date":"2014-03-29T07:02:26","date_gmt":"2014-03-29T07:02:26","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/29\/why-use-integers-for-tokens-collection-of-common-programming-errors\/"},"modified":"2014-03-29T07:02:26","modified_gmt":"2014-03-29T07:02:26","slug":"why-use-integers-for-tokens-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/29\/why-use-integers-for-tokens-collection-of-common-programming-errors\/","title":{"rendered":"Why use integers for tokens?-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li><img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/MbIFT.jpg?s=32&amp;g=1\" \/><br \/>\ndevoured elysium<\/p>\n<p>Is there any good reason for using numbers for identifying tokens, nowadays? I am following <code>Crafting a Compiler<\/code>.<\/p>\n<p>The code the author presents is here:<\/p>\n<pre><code>public class Token {\n    public final static int ID = 0, FLTDCL = 1, INTDCL = 2, PRINT = 3,\n            ASSIGN = 4, PLUS = 5, MINUS = 6, EOF = 7, INUM = 8, FNUM = 9;\n\n    public final static String[] token2str = new String[] { \"id\", \"fltdcl\",\n            \"intdcl\", \"print\", \"assign\", \"plus\", \"minus\", \"$\", \"inum\", \"fnum\" };\n\n    public final int type;\n    public final String val;\n\n    public Token(int type) {\n        this(type, \"\");\n    }\n\n    public Token(int type, String val) {\n        this.type = type;\n        this.val = val;\n    }\n\n    public String toString() {\n        return \"Token type\\t\" + token2str[type] + \"\\tval\\t\" + val;\n    }\n}\n<\/code><\/pre>\n<p>Instead of using the ugly arrays, wouldn&#8217;t it be smarter to modify the constructors to accept strings for the <code>type<\/code> variable instead of integers? Then we could get rid of<\/p>\n<pre><code>    public final static int ID = 0, FLTDCL = 1, INTDCL = 2, PRINT = 3,\n            ASSIGN = 4, PLUS = 5, MINUS = 6, EOF = 7, INUM = 8, FNUM = 9;\n<\/code><\/pre>\n<p>or is it needing later, being that using a string instead would be worse?<\/p>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/56e17cbc8c5ea5b37d67018db6d030b0?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nlarsmans<\/p>\n<p>There are several benefits:<\/p>\n<ul>\n<li>It&#8217;s faster, since comparing two integers takes (in your average compiled language) only a few instructions, while comparing strings takes O(<em>n<\/em>) time where <em>n<\/em> is the length of the larger token. Compilers need this extra bit of speed.<\/li>\n<li>In C, C++ and Java, you can <code>switch<\/code> on an <code>int<\/code> but not on a string.<\/li>\n<li>Mistyping a token name will be a compile-time error instead of a hard-to-debug runtime error.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>devoured elysium Is there any good reason for using numbers for identifying tokens, nowadays? I am following Crafting a Compiler. The code the author presents is here: public class Token { public final static int ID = 0, FLTDCL = 1, INTDCL = 2, PRINT = 3, ASSIGN = 4, PLUS = 5, MINUS = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3620","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3620","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3620"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3620\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3620"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3620"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3620"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}