{"id":846,"date":"2010-01-13T08:34:25","date_gmt":"2010-01-13T16:34:25","guid":{"rendered":"http:\/\/boost-spirit.com\/home\/2010\/01\/13\/whats-the-difference-between-the-components-a-lita-and-char_a\/"},"modified":"2010-01-20T15:36:21","modified_gmt":"2010-01-20T23:36:21","slug":"whats-the-difference-between-the-components-a-lita-and-char_a","status":"publish","type":"post","link":"http:\/\/boost-spirit.com\/home\/2010\/01\/13\/whats-the-difference-between-the-components-a-lita-and-char_a\/","title":{"rendered":"What&#8217;s the difference between the Spirit components &#8216;a&#8217;, lit(&#8216;a&#8217;), and char_(&#8216;a&#8217;)?"},"content":{"rendered":"<p>While looking through the mailing list archives I realized that often the small issues get into our way. Small snippets of information are making the difference. So I decided to start a (more or less regular) series about small tips helping to get your work done while using <em>Spirit<\/em>. Even if I sloppily call this series &#8216;Tip of the Day&#8217;, I by no means plan to have a tip a day.<\/p>\n<p>This time I&#8217;m going to highlight the difference between three different ways to parse or generate a single character: <span style=\"font-family: Courier New;\">&#8216;a&#8217;<\/span>, <span style=\"font-family: Courier New;\">lit(&#8216;a&#8217;)<\/span>, and <span style=\"font-family: Courier New;\">char_(&#8216;a&#8217;)<\/span>.<\/p>\n<p><!--more--><\/p>\n<p>The first two forms, <span style=\"font-family: Courier New;\">&#8216;a&#8217;<\/span> and <span style=\"font-family: Courier New;\">lit(&#8216;a&#8217;)<\/span> are semantically equivalent. Both create a component (a parser in <em>Qi<\/em> and a generator in <em>Karma<\/em>) handling the specified literal character, in this case the <span style=\"font-family: Courier New;\">&#8216;a&#8217;<\/span>. As <span style=\"font-family: Courier New;\">&#8216;a&#8217;<\/span> and <span style=\"font-family: Courier New;\">lit(&#8216;a&#8217;)<\/span> expose <span style=\"font-family: Courier New;\">unused_type<\/span> as their attribute they can be used to match or generate a single <span style=\"font-family: Courier New;\">&#8216;a&#8217;<\/span> without exposing or consuming an attribute value (remember, <span style=\"font-family: Courier New;\">unused_type<\/span> is Spirit&#8217;s fancy way of saying &#8216;I don&#8217;t care&#8217;). This property makes the literal components very useful for matching and emitting characters not contributing to the semantics, such as comments in parsed source code, commas in the list of arguments to a function, etc.<\/p>\n<p>You might ask: &#8216;Why do you have two forms of expressing literals, then?&#8217;. The answer is readability. The first form (<span style=\"font-family: Courier New;\">&#8216;a&#8217;<\/span>)\u00a0 is simpler, costs less keystrokes to write and clearly expresses the intent to handle this character. So it is the preferred way of doing things. But unfortunately this isn&#8217;t syntactically possible all the time. <em>Spirit<\/em> implements its domain specific embedded languages (DSEL&#8217;s) by directly utilizing the C++ language. This defines the constraints we have to work with. Let us have a look at the following example, where we want to match two characters in a row:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n'a' &gt;&gt; 'b'\r\n<\/pre>\n<p>This expression simply right shifts the bit pattern representing the character <span style=\"font-family: Courier New;\">&#8216;a&#8217;<\/span> by the number of bits defined by the bit pattern representing the character <span style=\"font-family: Courier New;\">&#8216;b&#8217;<\/span>. That is clearly not what we want! We need to have a way to tell the compiler that the expression has to be interpreted as a <em>Qi<\/em> parser. <em>Spirit<\/em> solves this dilemma by &#8216;tainting&#8217; at least one of the terms:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nboost::spirit::lit('a') &gt;&gt; 'b'\r\n<\/pre>\n<p>Now we have a valid parser expression as the compiler is forced to invoke the proper overload of the right-shift operator as exposed by <em>Spirit<\/em>.<\/p>\n<p>The third form, <span style=\"font-family: Courier New;\">char_(&#8216;a&#8217;)<\/span> distinguishes itself from the former ones by exposing its literal type as its attribute. This expressions used as a <em>Qi<\/em> parser will expose the <span style=\"font-family: Courier New;\">&#8216;a&#8217;<\/span> as the attribute value if it matched an <span style=\"font-family: Courier New;\">&#8216;a&#8217;<\/span> in the input. If it is used as a <em>Karma<\/em> generator it will succeed generating an <span style=\"font-family: Courier New;\">&#8216;a&#8217;<\/span> either if no attribute is supplied or if the attribute value is <span style=\"font-family: Courier New;\">&#8216;a&#8217;<\/span>, failing to generate otherwise.<\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li><a href=\"#\" class=\"sharing-anchor sd-button share-more\"><span>Share<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><div class=\"sharing-hidden\"><div class=\"inner\" style=\"display: none;\"><ul><li class=\"share-facebook\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-facebook-846\" class=\"share-facebook sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/2010\/01\/13\/whats-the-difference-between-the-components-a-lita-and-char_a\/?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\" ><span>Facebook<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-twitter-846\" class=\"share-twitter sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/2010\/01\/13\/whats-the-difference-between-the-components-a-lita-and-char_a\/?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\" ><span>Twitter<\/span><\/a><\/li><li class=\"share-end\"><\/li><li class=\"share-pinterest\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-pinterest-846\" class=\"share-pinterest sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/2010\/01\/13\/whats-the-difference-between-the-components-a-lita-and-char_a\/?share=pinterest\" target=\"_blank\" title=\"Click to share on Pinterest\" ><span>Pinterest<\/span><\/a><\/li><li class=\"share-linkedin\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-linkedin-846\" class=\"share-linkedin sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/2010\/01\/13\/whats-the-difference-between-the-components-a-lita-and-char_a\/?share=linkedin\" target=\"_blank\" title=\"Click to share on LinkedIn\" ><span>LinkedIn<\/span><\/a><\/li><li class=\"share-end\"><\/li><li class=\"share-reddit\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-reddit sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/2010\/01\/13\/whats-the-difference-between-the-components-a-lita-and-char_a\/?share=reddit\" target=\"_blank\" title=\"Click to share on Reddit\" ><span>Reddit<\/span><\/a><\/li><li class=\"share-tumblr\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-tumblr sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/2010\/01\/13\/whats-the-difference-between-the-components-a-lita-and-char_a\/?share=tumblr\" target=\"_blank\" title=\"Click to share on Tumblr\" ><span>Tumblr<\/span><\/a><\/li><li class=\"share-end\"><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>While looking through the mailing list archives I realized that often the small issues get into our way. Small snippets of information are making the difference. So I decided to start a (more or less regular) series about small tips helping to get your work done while using Spirit. Even if I sloppily call this [&hellip;]<\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li><a href=\"#\" class=\"sharing-anchor sd-button share-more\"><span>Share<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><div class=\"sharing-hidden\"><div class=\"inner\" style=\"display: none;\"><ul><li class=\"share-facebook\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-facebook-846\" class=\"share-facebook sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/2010\/01\/13\/whats-the-difference-between-the-components-a-lita-and-char_a\/?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\" ><span>Facebook<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-twitter-846\" class=\"share-twitter sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/2010\/01\/13\/whats-the-difference-between-the-components-a-lita-and-char_a\/?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\" ><span>Twitter<\/span><\/a><\/li><li class=\"share-end\"><\/li><li class=\"share-pinterest\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-pinterest-846\" class=\"share-pinterest sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/2010\/01\/13\/whats-the-difference-between-the-components-a-lita-and-char_a\/?share=pinterest\" target=\"_blank\" title=\"Click to share on Pinterest\" ><span>Pinterest<\/span><\/a><\/li><li class=\"share-linkedin\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-linkedin-846\" class=\"share-linkedin sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/2010\/01\/13\/whats-the-difference-between-the-components-a-lita-and-char_a\/?share=linkedin\" target=\"_blank\" title=\"Click to share on LinkedIn\" ><span>LinkedIn<\/span><\/a><\/li><li class=\"share-end\"><\/li><li class=\"share-reddit\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-reddit sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/2010\/01\/13\/whats-the-difference-between-the-components-a-lita-and-char_a\/?share=reddit\" target=\"_blank\" title=\"Click to share on Reddit\" ><span>Reddit<\/span><\/a><\/li><li class=\"share-tumblr\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-tumblr sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/2010\/01\/13\/whats-the-difference-between-the-components-a-lita-and-char_a\/?share=tumblr\" target=\"_blank\" title=\"Click to share on Tumblr\" ><span>Tumblr<\/span><\/a><\/li><li class=\"share-end\"><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div><\/div><\/div>","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_s2mail":"","spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[19,18],"tags":[7,8],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pIHdZ-dE","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/posts\/846"}],"collection":[{"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/comments?post=846"}],"version-history":[{"count":8,"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/posts\/846\/revisions"}],"predecessor-version":[{"id":913,"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/posts\/846\/revisions\/913"}],"wp:attachment":[{"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/media?parent=846"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/categories?post=846"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/tags?post=846"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}