{"id":150,"date":"2009-02-05T08:00:14","date_gmt":"2009-02-05T06:00:14","guid":{"rendered":"http:\/\/mariusbancila.ro\/blog\/?p=150"},"modified":"2009-02-25T08:30:35","modified_gmt":"2009-02-25T06:30:35","slug":"evaluating-expressions-part-3-building-the-ast","status":"publish","type":"post","link":"https:\/\/mariusbancila.ro\/blog\/2009\/02\/05\/evaluating-expressions-part-3-building-the-ast\/","title":{"rendered":"Evaluating Expressions &#8211; Part 3: Building the AST"},"content":{"rendered":"<p>In my <a href=\"http:\/\/mariusbancila.ro\/blog\/?p=149\">previous post<\/a> we&#8217;ve parsed an exception verifying whether it&#8217;s correct or not syntactically. But we still have to evaluate it. To be able to do that we&#8217;ll have to build an abstract syntax tree. This can be done by modifying the previous code and inserting semantic action. That means we do something more when we match productions.<\/p>\n<p>An abstract syntax tree is a binary tree. The inner nodes will represent operators and leafs will be numerical values.<\/p>\n<p>Here is how a node in the AST will look:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"vertical-align: middle;\" src=\"\/blog\/wp-content\/uploads\/2009\/02\/ast_node.png\" alt=\"AST node\" width=\"245\" height=\"51\" \/><\/p>\n<p>It is defined like this:<\/p>\n<pre class=\"prettyprint\">\r\nenum ASTNodeType \r\n{\r\n   Undefined,\r\n   OperatorPlus,\r\n   OperatorMinus,\r\n   OperatorMul,\r\n   OperatorDiv,\r\n   UnaryMinus,\r\n   NumberValue\r\n};\r\n\r\nclass ASTNode\r\n{\r\npublic:\r\n   ASTNodeType Type;\r\n   double      Value;\r\n   ASTNode*    Left;\r\n   ASTNode*    Right;\r\n\r\n   ASTNode()\r\n   {\r\n      Type = Undefined;\r\n      Value = 0;\r\n      Left = NULL;\r\n      Right = NULL;\r\n   }\r\n\r\n   ~ASTNode()\r\n   {\r\n      delete Left;\r\n      delete Right;\r\n   }\r\n};\r\n<\/pre>\n<p>For the expression 1+2*3, the AST will be:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"vertical-align: middle;\" src=\"\/blog\/wp-content\/uploads\/2009\/02\/ast_example.png\" alt=\"AST example\" width=\"665\" height=\"246\" \/><\/p>\n<p>We&#8217;ll build this tree by inserting semantic actions and adding nodes according to the following rules:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"vertical-align: middle;\" src=\"\/blog\/wp-content\/uploads\/2009\/02\/ast_semanticrules.png\" alt=\"AST semantic rules\" width=\"659\" height=\"229\" \/><\/p>\n<p>You&#8217;ll probably notice that based on these rules the AST shown above will be modified a little bit, with some additional nodes for operators + and *, having on the left a leaf node with the neutral element for the operation (zero for + and 1 for *), and on the right a node corresponding to a TERM or FACTOR. This won&#8217;t affect the evaluation.<\/p>\n<p>The Parser class will change so that the functions corresponding to the non-terminal symbols EXP, EXP1, TERM, TERM1 and FACTOR will return an ASTNode* instead of void. That is the node created as a semantic action.<\/p>\n<pre class=\"prettyprint\">\r\nclass Parser\r\n{\r\n   Token m_crtToken;\r\n   const char* m_Text;\r\n   size_t m_Index;\r\n\r\nprivate:\r\n\r\n   ASTNode* Expression()\r\n   {\r\n      ASTNode* tnode = Term();\r\n      ASTNode* e1node = Expression1();\r\n\r\n      return CreateNode(OperatorPlus, tnode, e1node);\r\n   }\r\n\r\n   ASTNode* Expression1()\r\n   {\r\n      ASTNode* tnode;\r\n      ASTNode* e1node;\r\n\r\n      switch(m_crtToken.Type)\r\n      {\r\n      case Plus:\r\n         GetNextToken();\r\n         tnode = Term();\r\n         e1node = Expression1();\r\n\r\n         return CreateNode(OperatorPlus, e1node, tnode);\r\n\r\n      case Minus:\r\n         GetNextToken();\r\n         tnode = Term();\r\n         e1node = Expression1();\r\n\r\n         return CreateNode(OperatorMinus, e1node, tnode);\r\n      }\r\n\r\n      return CreateNodeNumber(0);\r\n   }\r\n\r\n   ASTNode* Term()\r\n   {\r\n      ASTNode* fnode = Factor();\r\n      ASTNode* t1node = Term1();\r\n\r\n      return CreateNode(OperatorMul, fnode, t1node);\r\n   }\r\n\r\n   ASTNode* Term1()\r\n   {\r\n      ASTNode* fnode;\r\n      ASTNode* t1node;\r\n\r\n      switch(m_crtToken.Type)\r\n      {\r\n      case Mul: \r\n         GetNextToken();\r\n         fnode = Factor();\r\n         t1node = Term1();\r\n         return CreateNode(OperatorMul, t1node, fnode);\r\n\r\n      case Div:\r\n         GetNextToken();\r\n         fnode = Factor();\r\n         t1node = Term1();\r\n         return CreateNode(OperatorDiv, t1node, fnode);\r\n      }\r\n\r\n      return CreateNodeNumber(1);\r\n   }\r\n\r\n   ASTNode* Factor()\r\n   {\r\n      ASTNode* node;\r\n      switch(m_crtToken.Type)\r\n      {\r\n      case OpenParenthesis:\r\n         GetNextToken();\r\n         node = Expression();\r\n         Match(')');\r\n         return node;\r\n\r\n      case Minus:\r\n         GetNextToken();\r\n\t\t node = Factor();\r\n         return CreateUnaryNode(node);\r\n\r\n      case Number:\r\n         {\r\n            double value = m_crtToken.Value;\r\n            GetNextToken();\r\n            return CreateNodeNumber(value);\r\n         }\r\n\r\n      default:\r\n         {\r\n            std::stringstream sstr; \r\n            sstr &lt;&lt; \"Unexpected token '\" &lt;&lt; m_crtToken.Symbol &lt;&lt; \"' at position \" &lt;&lt; m_Index;\r\n            throw ParserException(sstr.str(), m_Index);\r\n         }\r\n      }\r\n   }\r\n\r\n   ASTNode* CreateNode(ASTNodeType type, ASTNode* left, ASTNode* right)\r\n   {\r\n      ASTNode* node = new ASTNode;\r\n      node->Type = type;\r\n      node->Left = left;\r\n      node->Right = right;\r\n\r\n      return node;\r\n   }\r\n\r\n   ASTNode* CreateUnaryNode(ASTNode* left)\r\n   {\r\n      ASTNode* node = new ASTNode;\r\n      node->Type = UnaryMinus;\r\n      node->Left = left;\r\n      node->Right = NULL;\r\n\r\n      return node;\r\n   }\r\n\r\n   ASTNode* CreateNodeNumber(double value)\r\n   {\r\n      ASTNode* node = new ASTNode;\r\n      node->Type = NumberValue;\r\n      node->Value = value;\r\n\r\n      return node;\r\n   }\r\n\r\n   void Match(char expected)\r\n   {\r\n      if(m_Text[m_Index-1] == expected)\r\n         GetNextToken();\r\n      else\r\n      {\r\n         std::stringstream sstr;\r\n         sstr &lt;&lt; \"Expected token '\" &lt;&lt; expected &lt;&lt; \"' at position \" &lt;&lt; m_Index;\r\n         throw ParserException(sstr.str(), m_Index);\r\n      }\r\n   }\r\n\r\n   void SkipWhitespaces()\r\n   {\r\n      while(isspace(m_Text[m_Index])) m_Index++;\r\n   }\r\n\r\n   void GetNextToken()\r\n   {\r\n      SkipWhitespaces();\r\n\r\n\t  m_crtToken.Value = 0;\r\n\t  m_crtToken.Symbol = 0;\r\n\r\n      if(m_Text[m_Index] == 0)\r\n      {\r\n         m_crtToken.Type = EndOfText;\r\n         return;\r\n      }\r\n\r\n      if(isdigit(m_Text[m_Index]))\r\n      {\r\n         m_crtToken.Type = Number;\r\n\t\t m_crtToken.Value = GetNumber();\r\n         return;\r\n      }\r\n\r\n      m_crtToken.Type = Error;\r\n\r\n      switch(m_Text[m_Index])\r\n      {\r\n      case '+': m_crtToken.Type = Plus; break;\r\n      case '-': m_crtToken.Type = Minus; break;\r\n      case '*': m_crtToken.Type = Mul; break;\r\n      case '\/': m_crtToken.Type = Div; break;\r\n      case '(': m_crtToken.Type = OpenParenthesis; break;\r\n      case ')': m_crtToken.Type = ClosedParenthesis; break;\r\n      }\r\n\r\n      if(m_crtToken.Type != Error)\r\n\t  {\r\n         m_crtToken.Symbol = m_Text[m_Index];\r\n         m_Index++;\r\n\t  }\r\n      else\r\n      {\r\n         std::stringstream sstr; \r\n         sstr &lt;&lt; \"Unexpected token '\" &lt;&lt; m_Text[m_Index] &lt;&lt; \"' at position \" &lt;&lt; m_Index;\r\n         throw ParserException(sstr.str(), m_Index);\r\n      }\r\n   }\r\n\r\n   double GetNumber()\r\n   {\r\n      SkipWhitespaces();\r\n\r\n      int index = m_Index;\r\n      while(isdigit(m_Text[m_Index])) m_Index++;\r\n      if(m_Text[m_Index] == '.') m_Index++;\r\n      while(isdigit(m_Text[m_Index])) m_Index++;\r\n\r\n      if(m_Index - index == 0)\r\n         throw ParserException(\"Number expected but not found!\", m_Index);\r\n\r\n      char buffer[32] = {0};\r\n      memcpy(buffer, &amp;m_Text[index], m_Index - index);\r\n\r\n      return atof(buffer);\r\n   }\r\n\r\npublic:\r\n   ASTNode* Parse(const char* text)\r\n   {\r\n      m_Text = text;\r\n      m_Index = 0;\r\n      GetNextToken();\r\n\r\n      return Expression();\r\n   }\r\n};\r\n<\/pre>\n<p>Now the Parse() method will return the created abstract syntax tree. We will see how to evaluate the expression by traversing this tree in the next post.<\/p>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my previous post we&#8217;ve parsed an exception verifying whether it&#8217;s correct or not syntactically. But we still have to evaluate it. To be able to do that we&#8217;ll have to build an abstract syntax tree. This can be done by modifying the previous code and inserting semantic action. That means we do something more &#8230; <a title=\"Evaluating Expressions &#8211; Part 3: Building the AST\" class=\"read-more\" href=\"https:\/\/mariusbancila.ro\/blog\/2009\/02\/05\/evaluating-expressions-part-3-building-the-ast\/\" aria-label=\"Read more about Evaluating Expressions &#8211; Part 3: Building the AST\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","advgb_blocks_editor_width":"","advgb_blocks_columns_visual_guide":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[9,7],"tags":[58,451,61,59,62],"class_list":["post-150","post","type-post","status-publish","format-standard","hentry","category-articles_and_tutorials","category-c","tag-ast","tag-c","tag-expression","tag-parser","tag-tree"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.1.1 - aioseo.com -->\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Marius Bancila\"\/>\n\t<meta name=\"keywords\" content=\"ast,c++,expression,parser,tree\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/mariusbancila.ro\/blog\/2009\/02\/05\/evaluating-expressions-part-3-building-the-ast\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.1.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Marius Bancila&#039;s Blog | About code. Mostly on C++\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Evaluating Expressions \u2013 Part 3: Building the AST\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/mariusbancila.ro\/blog\/2009\/02\/05\/evaluating-expressions-part-3-building-the-ast\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2009-02-05T06:00:14+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2009-02-25T06:30:35+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Evaluating Expressions \u2013 Part 3: Building the AST\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2009\\\/02\\\/05\\\/evaluating-expressions-part-3-building-the-ast\\\/#article\",\"name\":\"Evaluating Expressions \\u2013 Part 3: Building the AST\",\"headline\":\"Evaluating Expressions &#8211; Part 3: Building the AST\",\"author\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#organization\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"\\\/blog\\\/wp-content\\\/uploads\\\/2009\\\/02\\\/ast_node.png\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2009\\\/02\\\/05\\\/evaluating-expressions-part-3-building-the-ast\\\/#articleImage\"},\"datePublished\":\"2009-02-05T08:00:14+02:00\",\"dateModified\":\"2009-02-25T08:30:35+02:00\",\"inLanguage\":\"en-US\",\"commentCount\":3,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2009\\\/02\\\/05\\\/evaluating-expressions-part-3-building-the-ast\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2009\\\/02\\\/05\\\/evaluating-expressions-part-3-building-the-ast\\\/#webpage\"},\"articleSection\":\"Articles &amp; Tutorials, C++, AST, C++, expression, parser, tree\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2009\\\/02\\\/05\\\/evaluating-expressions-part-3-building-the-ast\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/#listItem\",\"name\":\"IT\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/#listItem\",\"position\":2,\"name\":\"IT\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/#listItem\",\"name\":\"Software\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/#listItem\",\"position\":3,\"name\":\"Software\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/#listItem\",\"name\":\"C++\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/#listItem\",\"name\":\"IT\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/#listItem\",\"position\":4,\"name\":\"C++\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2009\\\/02\\\/05\\\/evaluating-expressions-part-3-building-the-ast\\\/#listItem\",\"name\":\"Evaluating Expressions &#8211; Part 3: Building the AST\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/#listItem\",\"name\":\"Software\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2009\\\/02\\\/05\\\/evaluating-expressions-part-3-building-the-ast\\\/#listItem\",\"position\":5,\"name\":\"Evaluating Expressions &#8211; Part 3: Building the AST\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/#listItem\",\"name\":\"C++\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#organization\",\"name\":\"Marius Bancila's Blog\",\"description\":\"About code. Mostly on C++\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/\",\"name\":\"Marius Bancila\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2009\\\/02\\\/05\\\/evaluating-expressions-part-3-building-the-ast\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a84dd2831d955c38355ddea55df4df260809b88f36408bc14fd4eab8f7f131c9?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Marius Bancila\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2009\\\/02\\\/05\\\/evaluating-expressions-part-3-building-the-ast\\\/#webpage\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2009\\\/02\\\/05\\\/evaluating-expressions-part-3-building-the-ast\\\/\",\"name\":\"Evaluating Expressions \\u2013 Part 3: Building the AST\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2009\\\/02\\\/05\\\/evaluating-expressions-part-3-building-the-ast\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\"},\"datePublished\":\"2009-02-05T08:00:14+02:00\",\"dateModified\":\"2009-02-25T08:30:35+02:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/\",\"name\":\"Marius Bancila's Blog\",\"description\":\"About code. Mostly on C++\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Evaluating Expressions \u2013 Part 3: Building the AST","description":"","canonical_url":"https:\/\/mariusbancila.ro\/blog\/2009\/02\/05\/evaluating-expressions-part-3-building-the-ast\/","robots":"max-image-preview:large","keywords":"ast,c++,expression,parser,tree","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/mariusbancila.ro\/blog\/2009\/02\/05\/evaluating-expressions-part-3-building-the-ast\/#article","name":"Evaluating Expressions \u2013 Part 3: Building the AST","headline":"Evaluating Expressions &#8211; Part 3: Building the AST","author":{"@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author"},"publisher":{"@id":"https:\/\/mariusbancila.ro\/blog\/#organization"},"image":{"@type":"ImageObject","url":"\/blog\/wp-content\/uploads\/2009\/02\/ast_node.png","@id":"https:\/\/mariusbancila.ro\/blog\/2009\/02\/05\/evaluating-expressions-part-3-building-the-ast\/#articleImage"},"datePublished":"2009-02-05T08:00:14+02:00","dateModified":"2009-02-25T08:30:35+02:00","inLanguage":"en-US","commentCount":3,"mainEntityOfPage":{"@id":"https:\/\/mariusbancila.ro\/blog\/2009\/02\/05\/evaluating-expressions-part-3-building-the-ast\/#webpage"},"isPartOf":{"@id":"https:\/\/mariusbancila.ro\/blog\/2009\/02\/05\/evaluating-expressions-part-3-building-the-ast\/#webpage"},"articleSection":"Articles &amp; Tutorials, C++, AST, C++, expression, parser, tree"},{"@type":"BreadcrumbList","@id":"https:\/\/mariusbancila.ro\/blog\/2009\/02\/05\/evaluating-expressions-part-3-building-the-ast\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog#listItem","position":1,"name":"Home","item":"https:\/\/mariusbancila.ro\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/#listItem","name":"IT"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/#listItem","position":2,"name":"IT","item":"https:\/\/mariusbancila.ro\/blog\/category\/it\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/#listItem","name":"Software"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/#listItem","position":3,"name":"Software","item":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/#listItem","name":"C++"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/#listItem","name":"IT"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/#listItem","position":4,"name":"C++","item":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/2009\/02\/05\/evaluating-expressions-part-3-building-the-ast\/#listItem","name":"Evaluating Expressions &#8211; Part 3: Building the AST"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/#listItem","name":"Software"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/2009\/02\/05\/evaluating-expressions-part-3-building-the-ast\/#listItem","position":5,"name":"Evaluating Expressions &#8211; Part 3: Building the AST","previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/#listItem","name":"C++"}}]},{"@type":"Organization","@id":"https:\/\/mariusbancila.ro\/blog\/#organization","name":"Marius Bancila's Blog","description":"About code. Mostly on C++","url":"https:\/\/mariusbancila.ro\/blog\/"},{"@type":"Person","@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author","url":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/","name":"Marius Bancila","image":{"@type":"ImageObject","@id":"https:\/\/mariusbancila.ro\/blog\/2009\/02\/05\/evaluating-expressions-part-3-building-the-ast\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/a84dd2831d955c38355ddea55df4df260809b88f36408bc14fd4eab8f7f131c9?s=96&d=mm&r=g","width":96,"height":96,"caption":"Marius Bancila"}},{"@type":"WebPage","@id":"https:\/\/mariusbancila.ro\/blog\/2009\/02\/05\/evaluating-expressions-part-3-building-the-ast\/#webpage","url":"https:\/\/mariusbancila.ro\/blog\/2009\/02\/05\/evaluating-expressions-part-3-building-the-ast\/","name":"Evaluating Expressions \u2013 Part 3: Building the AST","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/mariusbancila.ro\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/mariusbancila.ro\/blog\/2009\/02\/05\/evaluating-expressions-part-3-building-the-ast\/#breadcrumblist"},"author":{"@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author"},"creator":{"@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author"},"datePublished":"2009-02-05T08:00:14+02:00","dateModified":"2009-02-25T08:30:35+02:00"},{"@type":"WebSite","@id":"https:\/\/mariusbancila.ro\/blog\/#website","url":"https:\/\/mariusbancila.ro\/blog\/","name":"Marius Bancila's Blog","description":"About code. Mostly on C++","inLanguage":"en-US","publisher":{"@id":"https:\/\/mariusbancila.ro\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"Marius Bancila's Blog | About code. Mostly on C++","og:type":"article","og:title":"Evaluating Expressions \u2013 Part 3: Building the AST","og:url":"https:\/\/mariusbancila.ro\/blog\/2009\/02\/05\/evaluating-expressions-part-3-building-the-ast\/","article:published_time":"2009-02-05T06:00:14+00:00","article:modified_time":"2009-02-25T06:30:35+00:00","twitter:card":"summary","twitter:title":"Evaluating Expressions \u2013 Part 3: Building the AST"},"aioseo_meta_data":{"post_id":"150","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"location":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2021-03-18 21:16:02","updated":"2025-12-12 07:24:40","seo_analyzer_scan_date":null,"focus_keyword":null,"additional_keywords":null,"truseo_locale":null},"author_meta":{"display_name":"Marius Bancila","author_link":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/"},"featured_img":null,"jetpack_publicize_connections":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Marius Bancila","author_link":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/"},"uagb_comment_info":3,"uagb_excerpt":"In my previous post we&#8217;ve parsed an exception verifying whether it&#8217;s correct or not syntactically. But we still have to evaluate it. To be able to do that we&#8217;ll have to build an abstract syntax tree. This can be done by modifying the previous code and inserting semantic action. That means we do something more&hellip;","coauthors":[],"tax_additional":{"categories":{"linked":["<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/articles_and_tutorials\/\" class=\"advgb-post-tax-term\">Articles &amp; Tutorials<\/a>","<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">C++<\/a>"],"unlinked":["<span class=\"advgb-post-tax-term\">Articles &amp; Tutorials<\/span>","<span class=\"advgb-post-tax-term\">C++<\/span>"]},"tags":{"linked":["<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">AST<\/a>","<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">C++<\/a>","<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">expression<\/a>","<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">parser<\/a>","<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">tree<\/a>"],"unlinked":["<span class=\"advgb-post-tax-term\">AST<\/span>","<span class=\"advgb-post-tax-term\">C++<\/span>","<span class=\"advgb-post-tax-term\">expression<\/span>","<span class=\"advgb-post-tax-term\">parser<\/span>","<span class=\"advgb-post-tax-term\">tree<\/span>"]}},"comment_count":"3","relative_dates":{"created":"Posted 18 years ago","modified":"Updated 18 years ago"},"absolute_dates":{"created":"Posted on February 5, 2009","modified":"Updated on February 25, 2009"},"absolute_dates_time":{"created":"Posted on February 5, 2009 8:00 am","modified":"Updated on February 25, 2009 8:30 am"},"featured_img_caption":"","series_order":"","jetpack_shortlink":"https:\/\/wp.me\/pYNdv-2q","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts\/150","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/comments?post=150"}],"version-history":[{"count":1,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts\/150\/revisions"}],"predecessor-version":[{"id":192,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts\/150\/revisions\/192"}],"wp:attachment":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/media?parent=150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/categories?post=150"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/tags?post=150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}