{"id":37,"date":"2024-08-01T18:35:48","date_gmt":"2024-08-01T18:35:48","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalC%2B%2BProgramming\/?page_id=37"},"modified":"2025-01-20T21:47:49","modified_gmt":"2025-01-20T21:47:49","slug":"assignment-7","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/assignments\/assignment-7\/","title":{"rendered":"Assignment 7"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Abstract_Syntax_Trees\"><\/span>Abstract Syntax Trees<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Description\"><\/span><em>Description<\/em><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Various programs (such as compilers and calculators), use a data structure called an&nbsp;<strong><em>Abstract Syntax Tree (AST)<\/em><\/strong>&nbsp;to represent syntax that has been recognized by the program.<\/p>\n\n\n\n<div style=\"height:13px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>NOTE:<\/strong><\/p>\n\n\n\n<p>An Abstract Syntax Tree is&nbsp;<strong>not&nbsp;<\/strong>the same as a Binary Search Tree.&nbsp;<\/p>\n\n\n\n<p>In a Binary Search Tree, both interior and leaf nodes can contain data, and the nodes are ordered in such a way that the data can be found by searching the tree in a prescribed order. In an Abstract Syntax Tree, the interior nodes contain&nbsp;<em>operator&nbsp;<\/em>information, while the leaf nodes contain&nbsp;<em>operand&nbsp;<\/em>information.&nbsp;<\/p>\n\n\n\n<p>The order of the nodes within a Binary Search Tree is determined by the contents of each node. The order of the nodes in an AST is determined by the expression that the AST represents, and by the precedence of the expression&#8217;s components.<\/p>\n<\/blockquote>\n\n\n\n<p>For example, the arithmetic expression:<\/p>\n\n\n\n<pre class=\"wp-block-code has-medium-font-size\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>-5+12*4<\/strong><\/code><\/pre>\n\n\n\n<p>would be represented in Abstract Syntax Tree form as:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"346\" height=\"188\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/wp-content\/uploads\/2024\/11\/expr1.gif\" alt=\"\" class=\"wp-image-1623\"\/><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>Note<\/strong><em><strong>:<\/strong><\/em>&nbsp;Conventionally, trees in Computer Science are drawn upside down when compared to trees in the real world. Thus, the&nbsp;<em>root<\/em>&nbsp;of the tree is at the top, not the bottom.<\/p>\n<\/blockquote>\n\n\n\n<p>An arithmetic expression uses a&nbsp;<em>convention&nbsp;<\/em>to distinguish between the different possible interpretations. For example, the expression:<\/p>\n\n\n\n<pre class=\"wp-block-code has-medium-font-size\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>-5+12*4<\/strong><\/code><\/pre>\n\n\n\n<p>could be interpreted as either:<\/p>\n\n\n\n<pre class=\"wp-block-code has-medium-font-size\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>(-5+12)*4<\/strong><\/code><\/pre>\n\n\n\n<p>or:<\/p>\n\n\n\n<pre class=\"wp-block-code has-medium-font-size\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>-5+(12*4)<\/strong><\/code><\/pre>\n\n\n\n<p>The latter is the conventionally correct interpretation, because the multiplication operator conventionally has&nbsp;<em>higher precedence<\/em>&nbsp;than the addition operator. However, the Abstract Syntax Tree representation is&nbsp;<em>unambiguous<\/em>, which is one of the reasons it is so popular.<\/p>\n\n\n\n<p>In the above AST diagram, each element in the tree is connected to other items in the tree by lines. An element in an Abstract Syntax Tree is usually called a&nbsp;<em>Node<\/em>. Nodes can either be&nbsp;<em>interior&nbsp;nodes<\/em>&nbsp;or&nbsp;<em>leaf&nbsp;nodes<\/em>. Interior nodes are those that have one or more lines connecting them with other nodes further down the tree. Leaf nodes have no such connections; they are the lowest nodes in that particular branch of the tree. We say that interior nodes have&nbsp;<em>child<\/em>&nbsp;nodes, while leaf nodes do not.<\/p>\n\n\n\n<p>In this example, interior nodes are used to represent the&nbsp;<em>arithmetic operators<\/em>&nbsp;(+,-,*,\/, etc.). Arithmetic operators can be&nbsp;<em>unary<\/em>&nbsp;(operating on a single operand), or&nbsp;<em>binary<\/em>&nbsp;(operating on two operands). A unary operator has exactly one child node, while a binary operator has exactly two child nodes. For example, the minus sign in the above expression represents the&nbsp;<em>unary minus<\/em>&nbsp;operator, while the asterisk represents the&nbsp;<em>binary multiplication<\/em>&nbsp;operator, and the plus sign represents the&nbsp;<em>binary addition<\/em>&nbsp;operator.<\/p>\n\n\n\n<p>A node below an operand in the tree can represent a&nbsp;<em>value<\/em>&nbsp;such as&nbsp;5&nbsp;or&nbsp;12&nbsp;(in the above diagram, all values are integer values). If the node represents a simple value, then it must be a leaf node. If it represents an expression (consisting of some combination of operators and operands), then the node must represent an operator, and therefore must be an interior node.<\/p>\n\n\n\n<p>For example, imagine you wish to modify the original expression to look like this:<\/p>\n\n\n\n<pre class=\"wp-block-code has-medium-font-size\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>-(5\/23)+12*(4-2)<\/strong><\/code><\/pre>\n\n\n\n<p>The Abstract Syntax Tree for this new expression looks like:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"381\" height=\"244\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/wp-content\/uploads\/2024\/11\/expr2.gif\" alt=\"\" class=\"wp-image-1626\"\/><\/figure>\n\n\n\n<p>Note that we have replaced the original leaf nodes for&nbsp;5&nbsp;and&nbsp;4&nbsp;by&nbsp;<em>subtrees<\/em>&nbsp;which represent the new&nbsp;<em>subexpressions<\/em>. The top node in each of these subexpressions represents an&nbsp;<em>operator<\/em>; which operator depends on the relative precedence of the operators in the subexpression.<\/p>\n\n\n\n<p>A program can&nbsp;<em>evaluate<\/em>&nbsp;such a tree by&nbsp;<em>walking<\/em>&nbsp;the tree in a prescribed order: at each interior node it determines the value from each subtree, and applies the operator to those values to produce a value for the tree represented by that interior node. A leaf node provides a value directly. A program can also walk the tree in the same fashion to produce a printout of the equivalent arithmetic expression (although it would have to insert parentheses appropriately to maintain the proper operator precedence).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_Assignment\"><\/span><em>The Assignment<\/em><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Your job is to write a set of classes which will allow you to construct such an abstract syntax tree, and then walk it, and evaluate it. Here&#8217;s an abstract&nbsp;<strong>Node&nbsp;<\/strong>class to start with:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ The Node base class\nclass Node\n{\npublic:\n  virtual ~Node() {}\n\n  virtual double Evaluate() const = 0;\n  virtual void Walk() const = 0;\n};\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">\/\/ The Node base class<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">class<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">Node<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">public:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">virtual<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">~Node<\/span><span style=\"color: #000000\">() {}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">virtual<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Evaluate<\/span><span style=\"color: #000000\">() <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> = <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">virtual<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Walk<\/span><span style=\"color: #000000\">() <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> = <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">};<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>This class is to be used as a base class for a family of node classes which can be used to construct an AST and then evaluate and walk it.&nbsp; Cut it from this web page and paste it into your editor (enclosing it with the usual requirements for a header file).<\/p>\n<\/div>\n<\/div>\n\n\n\n<p>The member function&nbsp;<strong><code>Evaluate()<\/code><\/strong>&nbsp;returns the value for the (sub)tree, starting from that node on down. (Notice that it returns a&nbsp;double, not an&nbsp;int. This is because we would like to be able to evaluate expressions that contain more than integer values &#8212; see later.) <\/p>\n\n\n\n<p>For example, calling&nbsp;<strong><code>Evaluate()<\/code><\/strong>&nbsp;for the node that represents the binary multiplication operator,&nbsp;<strong><code>*<\/code><\/strong>, in the original expression, will return a value of&nbsp;<code>48.0<\/code>&nbsp;(12*4). Calling&nbsp;<strong><code>Evaluate()<\/code><\/strong>&nbsp;for the node at the top of the tree will return the value of the entire expression.<\/p>\n\n\n\n<p>The member function&nbsp;<strong><code>Walk()<\/code><\/strong>&nbsp;prints out (on&nbsp;<code><strong>cout<\/strong><\/code>) the arithmetic expression represented by that (sub)tree. In the original example expression, calling&nbsp;<strong><code>Walk()<\/code><\/strong>&nbsp;for the node that represents the unary minus operator will print out:<\/p>\n\n\n\n<pre class=\"wp-block-code has-medium-font-size\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>-5<\/strong><\/code><\/pre>\n\n\n\n<p>while calling&nbsp;<strong><code>Walk()<\/code><\/strong> for the node that represents the binary multiplication operator will print out:<\/p>\n\n\n\n<pre class=\"wp-block-code has-medium-font-size\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>12 * 4<\/strong><\/code><\/pre>\n\n\n\n<p>Calling&nbsp;<strong><code>Walk()<\/code><\/strong> for the node at the top of the tree will print out the entire expression:<\/p>\n\n\n\n<pre class=\"wp-block-code has-medium-font-size\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>-5 + 12 * 4<\/strong><\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>Questions:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><em><strong>What makes the Node class abstract?<\/strong><\/em><\/li>\n\n\n\n<li><em><strong>Why must Node::~Node() be virtual?<\/strong><\/em><\/li>\n<\/ul>\n<\/blockquote>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Part_A_Integer_UnaryMinus_Plus_and_Times\"><\/span><em>Part A: Integer, UnaryMinus, Plus and Times<\/em><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>The first task is to write a set of classes:&nbsp;<strong>Integer<\/strong>,&nbsp;<strong>UnaryMinus<\/strong>,&nbsp;<strong>Plus<\/strong>&nbsp;and&nbsp;<strong>Times<\/strong>, which use&nbsp;<em>single inheritance<\/em>&nbsp;and&nbsp;<em>virtual functions<\/em>, and which can be used in the following main program:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/\n\/\/  Syntax.cpp\n\/\/  Abstract Syntax Trees\n\/\/\n\/\/  Created by Bryan Higgs on 11\/5\/24.\n\/\/\n\n#include &lt;iostream&gt;\nusing namespace std;\n\n#include &quot;Nodes.h&quot;\n\nint main(int argc, const char * argv[])\n{\n  Node *expression = \/\/ AST represents: -5 + 12 * 4\n           new Plus(                    \/\/ +\n                 new UnaryMinus(        \/\/   -\n                       new Integer(5)   \/\/    5\n                       ),\n                 new Times(             \/\/   *\n                       new Integer(12), \/\/    12\n                       new Integer(4)   \/\/    4\n                       )\n                 );\n\n  double result = expression-&gt;Evaluate();\n\n  cout &lt;&lt; &quot;Result of &quot;;\n  expression-&gt;Walk();\n  cout &lt;&lt; &quot; = &quot; &lt;&lt; result &lt;&lt; endl;\n\n  delete expression; \/\/ Clean up expression AST\n\n  \/\/ Test additional expression functionality\n\n  expression = \/\/ AST represents: -(5 + 12 * 4)\n    new UnaryMinus(\n     new Plus(\n    new Integer(5),\n    new Times(\n      new Integer(12),\n      new Integer(4)\n      )\n    )\n  );\n\n  result = expression-&gt;Evaluate();\n  cout &lt;&lt; &quot;Result of &quot;;\n  expression-&gt;Walk();\n  cout &lt;&lt; &quot; = &quot; &lt;&lt; result &lt;&lt; endl;\n\n  delete expression; \/\/ Clean up expression AST\n\n  return 0;\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  Syntax.cpp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  Abstract Syntax Trees<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  Created by Bryan Higgs on 11\/5\/24.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;iostream&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">using<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">namespace<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&quot;Nodes.h&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">argc<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">*<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">argv<\/span><span style=\"color: #000000\">[])<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  Node *expression =<\/span><span style=\"color: #008000\"> \/\/ AST represents: -5 + 12 * 4<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">           <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Plus<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #008000\">                    \/\/ +<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                 <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">UnaryMinus<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #008000\">        \/\/   -<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                       <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Integer<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">5<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #008000\">   \/\/    5<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                       ),<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                 <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Times<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #008000\">             \/\/   *<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                       <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Integer<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">12<\/span><span style=\"color: #000000\">),<\/span><span style=\"color: #008000\"> \/\/    12<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                       <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Integer<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">4<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #008000\">   \/\/    4<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                       )<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                 );<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> result = <\/span><span style=\"color: #001080\">expression<\/span><span style=\"color: #000000\">-&gt;<\/span><span style=\"color: #795E26\">Evaluate<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;Result of &quot;<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">expression<\/span><span style=\"color: #000000\">-&gt;<\/span><span style=\"color: #795E26\">Walk<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot; = &quot;<\/span><span style=\"color: #000000\"> &lt;&lt; result &lt;&lt; endl;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">delete<\/span><span style=\"color: #000000\"> expression;<\/span><span style=\"color: #008000\"> \/\/ Clean up expression AST<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">  \/\/ Test additional expression functionality<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  expression =<\/span><span style=\"color: #008000\"> \/\/ AST represents: -(5 + 12 * 4)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">UnaryMinus<\/span><span style=\"color: #000000\">(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">     <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Plus<\/span><span style=\"color: #000000\">(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Integer<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">5<\/span><span style=\"color: #000000\">),<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Times<\/span><span style=\"color: #000000\">(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">      <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Integer<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">12<\/span><span style=\"color: #000000\">),<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">      <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Integer<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">4<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">      )<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    )<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  );<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  result = <\/span><span style=\"color: #001080\">expression<\/span><span style=\"color: #000000\">-&gt;<\/span><span style=\"color: #795E26\">Evaluate<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;Result of &quot;<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">expression<\/span><span style=\"color: #000000\">-&gt;<\/span><span style=\"color: #795E26\">Walk<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot; = &quot;<\/span><span style=\"color: #000000\"> &lt;&lt; result &lt;&lt; endl;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">delete<\/span><span style=\"color: #000000\"> expression;<\/span><span style=\"color: #008000\"> \/\/ Clean up expression AST<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em><strong>Note:<\/strong><\/em><strong>&nbsp;<\/strong><\/p>\n\n\n\n<p>In all of the classes you write, be sure to implement any non-trivial member functions&nbsp;<em>in a separate .cpp file<\/em>,&nbsp;<em>not in the .h file.&nbsp;&nbsp;<\/em>&nbsp;<\/p>\n\n\n\n<p>In Part 1, this will be in file&nbsp;<code>nodes.cpp<\/code><\/p>\n\n\n\n<p>In Part 2, in file&nbsp;<code>extnodes.cpp<\/code>. Any code depending on&nbsp;<code>&lt;iostream&gt;<\/code>&nbsp;should be placed in the .cpp file.<\/p>\n<\/blockquote>\n<\/div>\n<\/div>\n\n\n\n<h5 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Sequence_of_steps_to_perform\"><\/span><em>Sequence of steps to perform:<\/em><span class=\"ez-toc-section-end\"><\/span><\/h5>\n\n\n\n<ol class=\"wp-block-list\">\n<li>In&nbsp;nodes.h, write a class&nbsp;<strong><code>Integer<\/code><\/strong> which is&nbsp;<em>publicly derived<\/em>&nbsp;from class&nbsp;<strong><code>Node<\/code><\/strong>.&nbsp; The&nbsp; class will have a data member which will contain the integer value for that instance of&nbsp;<strong><code>Integer<\/code><\/strong>. The class will also have a constructor which allows an integer value to be passed in as a parameter, and virtual functions&nbsp;<strong><code>Evaluate()<\/code><\/strong>&nbsp;and&nbsp;<strong><code>Walk()<\/code><\/strong>.&nbsp; Instances of class&nbsp;<strong><code>Integer<\/code><\/strong>&nbsp;will be leaf nodes in the Abstract Syntax Tree.&nbsp; Any non-inline member function implementations should go in file&nbsp;<code>nodes.cpp<\/code>.<\/li>\n\n\n\n<li>Now it&#8217;s time to look at interior (non-leaf) nodes: the operators. As mentioned before, these break down into two subclasses &#8212;&nbsp;<em>unary<\/em>&nbsp;and&nbsp;<em>binary&nbsp;<\/em>operators. However, all operators share some degree of commonality, so, first, create an&nbsp;<em>abstract<\/em>&nbsp;class&nbsp;<strong><code>Operator<\/code><\/strong>, which is&nbsp;<em>publicly derived<\/em>&nbsp;from&nbsp;<strong><code>Node<\/code><\/strong>, and which contains nothing but a single&nbsp;<em>protected pure virtual member function<\/em>: <br><strong>virtual const char *Name() const = 0;<\/strong><br>This will force every non-abstract class that derives from the&nbsp;<strong>Operator<\/strong> class to implement a virtual function with this signature. The&nbsp;<strong><code>Name() <\/code><\/strong>virtual member function is used to output the operator&#8217;s symbol (<strong><code>+<\/code><\/strong>,&nbsp;<strong><code>-<\/code><\/strong>,&nbsp;<strong><code>*<\/code><\/strong>, etc.). The&nbsp;<strong><code>Name()<\/code><\/strong><br>function will be called from the&nbsp;<strong><code>Walk()<\/code><\/strong> function (see later). (Note that you can add appropriate spacing around the operator name to obtain pleasing results when the expression is output.)<\/li>\n\n\n\n<li>All unary operators share some common attributes. Naturally, every unary operator is an&nbsp;<strong><code>Operator<\/code><\/strong>. In particular, each unary operator needs only a&nbsp;<em>single<\/em>&nbsp;<em>pointer<\/em>&nbsp;to its operand (child) node. Create an&nbsp;<em>abstract<\/em>&nbsp;class,&nbsp;<strong><code>UnaryOperator<\/code><\/strong>, derived from&nbsp;<strong><code>Operator<\/code><\/strong>. This class will serve as the&nbsp;<em>base class for all unary operators<\/em>. It will contain data members and member functions common to all unary operators.<br>The&nbsp;<strong><code>UnaryOperator<\/code><\/strong> class should have a single data member to point to its operand.&nbsp;<em>What type should this data member be? (Hint: read the above paragraph carefully.)&nbsp;<\/em>Make this data member&nbsp;<code>private<\/code>! Provide a&nbsp;protected<br>member function (with an appropriate name) to allow derived classes to&nbsp;<em>access<\/em>, but not&nbsp;<em>modify<\/em>&nbsp;it. Make the constructor and destructor for&nbsp;<strong><code>UnaryOperator<\/code><\/strong> be&nbsp;<code>protected<\/code>. <br><em><strong>Note:<\/strong><\/em><strong>&nbsp;<\/strong><em>The test program assumes that the tree may be cleaned up by performing a&nbsp;delete. The easiest way to accomplish this is to assume that every <strong><code>Node<\/code><\/strong> will be allocated (<code>new<\/code>) from the heap, and so can be validly deleted. Do the appropriate&nbsp;delete(s) in the&nbsp;<strong><code>UnaryOperator<\/code><\/strong> destructor.<\/em><br>Implement the&nbsp;<strong><code>Walk()<\/code><\/strong> virtual member function in the&nbsp;<strong><code>UnaryOperator<\/code><\/strong> class.<em>&nbsp;[Why there?]<\/em>&nbsp; Make it&nbsp;protected.&nbsp;<br><em><strong>Hint: <\/strong>This&nbsp;<strong><code>Walk()<\/code><\/strong> function will make recursive calls to itself and other virtual&nbsp;<strong><code>Walk()<\/code><\/strong>&nbsp;member functions.<\/em><\/li>\n\n\n\n<li>Create the concrete (non-abstract) class&nbsp;<strong><code>UnaryMinus<\/code><\/strong>, derived from&nbsp;<strong><code>UnaryOperator<\/code><\/strong>. It should have a very simple&nbsp;<code>public<\/code> constructor (<strong><em>with what for arguments?<\/em><\/strong>). Implement the&nbsp;<strong><code>Evaluate()<\/code><\/strong> and&nbsp;<strong><code>Name()<\/code><\/strong> virtual functions for this class.<\/li>\n\n\n\n<li>All binary operators share some common attributes. Naturally, every binary operator is an&nbsp;<strong><code>Operator<\/code><\/strong>. In addition, each binary operator needs&nbsp;<em>two pointers<\/em>&nbsp;&#8212; one to its left operand (child) node, and one to its right operand (child) node. Create an&nbsp;<em>abstract<\/em>&nbsp;class,&nbsp;<strong><code>BinaryOperator<\/code><\/strong>, publicly derived from&nbsp;<strong><code>Operator<\/code><\/strong>. This class will serve as the base class for all binary operators. It will contain data members and member functions common to all binary operators.<br>The&nbsp;<strong>BinaryOperator<\/strong> class should have a two data members.&nbsp;<strong><em>What type should each one be?<\/em><\/strong>&nbsp;<br><strong><em>Hint: <\/em><\/strong><em>read the above paragraph carefully.&nbsp;<\/em><br>Make these data members&nbsp;<code>private<\/code>. Provide&nbsp;<code>protected<\/code>&nbsp;access member functions (with appropriate names) to allow derived classes to access, but not modify, them. Make the constructor and destructor for&nbsp;<strong><code>BinaryOperator<\/code><\/strong> be&nbsp;protected.<br><em><strong>Note:<\/strong><\/em><strong>&nbsp;<\/strong><em>The test program assumes that the tree may be cleaned up by performing a&nbsp;delete. The easiest way to accomplish this is to assume that every Node will be allocated (<code>new<\/code>) from the heap, and so can be validly deleted. Do the appropriate&nbsp;delete(s) in the&nbsp;<strong><code>UnaryOperator<\/code><\/strong>&nbsp;destructor.<\/em><br>Implement the&nbsp;<strong><code>Walk()<\/code><\/strong>&nbsp;virtual member function in the&nbsp;<strong><code>BinaryOperator<\/code><\/strong>&nbsp;class. Make it&nbsp;<code>protected<\/code>.&nbsp;<br><em><strong>Hint: <\/strong>This&nbsp;Walk()&nbsp;function will make recursive calls to itself and other virtual&nbsp;Walk()&nbsp;member functions.<\/em><\/li>\n\n\n\n<li>Create the classes&nbsp;<strong><code>Plus<\/code><\/strong> and&nbsp;<strong><code>Times<\/code><\/strong>, each publicly derived from&nbsp;<strong><code>BinaryOperator<\/code><\/strong>. They should have a very simple public constructor (<strong><em>with what for arguments?<\/em><\/strong>)&nbsp;&nbsp;Implement the&nbsp;<strong><code>Evaluate()<\/code><\/strong>&nbsp;and&nbsp;<strong><code>Name()<\/code><\/strong>&nbsp;virtual functions for these classes.<\/li>\n<\/ol>\n\n\n\n<p>By now, you should be able to compile and link the entire&nbsp;syntax&nbsp;program, and run it. (Note that you will have to compile and link together two&nbsp;.cpp&nbsp;files)<\/p>\n\n\n\n<p><strong><em>Does it work?<\/em><\/strong><em><strong>&nbsp;If so, do you understand how?<\/strong><\/em><\/p>\n\n\n\n<p>My version produced the following output for the first expression:<\/p>\n\n\n\n<pre class=\"wp-block-code has-medium-font-size\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>Result of -5 + 12 * 4 = 43.0<\/strong><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Part_B_Adding_More_Node_Types\"><\/span><em>Part B<\/em><em>: Adding More Node Types<\/em><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>Requirement:<\/strong>&nbsp;<\/p>\n\n\n\n<p>From now on, consider all the&nbsp;<strong>.h<\/strong>&nbsp;and&nbsp;<strong>.cpp<\/strong>&nbsp;files you developed in Part A&nbsp;<em>locked<\/em>. This means that&nbsp;<em>you cannot add or change anything in those files<\/em>.<\/p>\n<\/blockquote>\n\n\n\n<p>Create new files&nbsp;<code>extnodes.h<\/code>&nbsp;and&nbsp;<code>extnodes.cpp<\/code>, and add the additional node classes to them (<em>not<\/em> to <code>nodes.h<\/code> or <code>nodes.cpp<\/code>)<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>A&nbsp;<strong><code>Real<\/code>&nbsp;<\/strong>class that will hold floating point (double) values.<\/li>\n\n\n\n<li>A&nbsp;<strong><code>UnaryPlus<\/code>&nbsp;<\/strong>class.<\/li>\n\n\n\n<li>A&nbsp;<strong><code>Minus<\/code>&nbsp;<\/strong>class.<\/li>\n\n\n\n<li>A&nbsp;<strong><code>DividedBy<\/code>&nbsp;<\/strong>class<\/li>\n<\/ol>\n\n\n\n<p>Here&#8217;s an extended test program to test your work:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:60%\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/\n\/\/  Syntax2.cpp\n\/\/  Abstract Syntax Trees\n\/\/\n\/\/  Created by Bryan Higgs on 11\/5\/24.\n\/\/\n\n\/\/ Test evaluation of an abstract syntax tree.\n\n#include &lt;iostream&gt;\nusing namespace std;\n\n#include &quot;Nodes.h&quot;\n#include &quot;extnodes.h&quot;   \/\/ For Real, UnaryPlus, Minus and DividedBy\n\nint main(int argc, const char * argv[])\n{\n  Node *expression = \/\/ AST represents: -5 + 12 * 4\n           new Plus(                    \/\/ +\n                 new UnaryMinus(        \/\/   -\n                       new Integer(5)   \/\/    5\n                       ),\n                 new Times(             \/\/   *\n                       new Integer(12), \/\/    12\n                       new Integer(4)   \/\/    4\n                       )\n                 );\n\n  double result = expression-&gt;Evaluate();\n\n  cout &lt;&lt; &quot;Result of &quot;;\n  expression-&gt;Walk();\n  cout &lt;&lt; &quot; = &quot; &lt;&lt; result &lt;&lt; endl;\n\n  delete expression; \/\/ Clean up expression AST\n\n   \/\/ Test additional expression functionality\n\n  expression = \/\/ AST represents: 53.5 \/ 45 - 73.54 * (+3 + 8)\n           new Minus(\n                 new DividedBy(\n                       new Real(53.5),\n                       new Integer(45)\n                       ),\n                 new Times(\n                       new Real(73.54),\n                       new Plus(\n                             new UnaryPlus(\n                                   new Integer(3)\n                                   ),\n                             new Integer(8)\n                             )\n                       )\n                 );\n\n  result = expression-&gt;Evaluate();\n  cout &lt;&lt; &quot;Result of &quot;;\n  expression-&gt;Walk();\n  cout &lt;&lt; &quot; = &quot; &lt;&lt; result &lt;&lt; endl;\n\n  delete expression; \/\/ Clean up expression AST\n\n   \/\/ Another expression\n\n  expression = \/\/ AST represents: (53.5 - 45) * (73.54 + -3 + 8)\n           new Times(\n                 new Minus(\n                       new Real(53.5),\n                       new Integer(45)\n                       ),\n                 new Plus(\n                       new Real(73.54),\n                       new Plus(\n                             new UnaryMinus(\n                                   new Integer(3)\n                                   ),\n                             new Integer(8)\n                             )\n                       )\n                 );\n\n  result = expression-&gt;Evaluate();\n  cout &lt;&lt; &quot;Result of &quot;;\n  expression-&gt;Walk();\n  cout &lt;&lt; &quot; = &quot; &lt;&lt; result &lt;&lt; endl;\n\n  delete expression; \/\/ Clean up expression AST\n\n  return 0;\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  Syntax2.cpp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  Abstract Syntax Trees<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  Created by Bryan Higgs on 11\/5\/24.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/ Test evaluation of an abstract syntax tree.<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;iostream&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">using<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">namespace<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&quot;Nodes.h&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&quot;extnodes.h&quot;<\/span><span style=\"color: #0000FF\">   <\/span><span style=\"color: #008000\">\/\/ For Real, UnaryPlus, Minus and DividedBy<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">argc<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">*<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">argv<\/span><span style=\"color: #000000\">[])<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  Node *expression =<\/span><span style=\"color: #008000\"> \/\/ AST represents: -5 + 12 * 4<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">           <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Plus<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #008000\">                    \/\/ +<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                 <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">UnaryMinus<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #008000\">        \/\/   -<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                       <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Integer<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">5<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #008000\">   \/\/    5<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                       ),<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                 <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Times<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #008000\">             \/\/   *<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                       <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Integer<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">12<\/span><span style=\"color: #000000\">),<\/span><span style=\"color: #008000\"> \/\/    12<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                       <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Integer<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">4<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #008000\">   \/\/    4<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                       )<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                 );<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> result = <\/span><span style=\"color: #001080\">expression<\/span><span style=\"color: #000000\">-&gt;<\/span><span style=\"color: #795E26\">Evaluate<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;Result of &quot;<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">expression<\/span><span style=\"color: #000000\">-&gt;<\/span><span style=\"color: #795E26\">Walk<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot; = &quot;<\/span><span style=\"color: #000000\"> &lt;&lt; result &lt;&lt; endl;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">delete<\/span><span style=\"color: #000000\"> expression;<\/span><span style=\"color: #008000\"> \/\/ Clean up expression AST<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">   \/\/ Test additional expression functionality<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  expression =<\/span><span style=\"color: #008000\"> \/\/ AST represents: 53.5 \/ 45 - 73.54 * (+3 + 8)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">           <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Minus<\/span><span style=\"color: #000000\">(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                 <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">DividedBy<\/span><span style=\"color: #000000\">(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                       <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Real<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">53.5<\/span><span style=\"color: #000000\">),<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                       <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Integer<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">45<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                       ),<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                 <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Times<\/span><span style=\"color: #000000\">(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                       <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Real<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">73.54<\/span><span style=\"color: #000000\">),<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                       <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Plus<\/span><span style=\"color: #000000\">(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                             <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">UnaryPlus<\/span><span style=\"color: #000000\">(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                                   <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Integer<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">3<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                                   ),<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                             <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Integer<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">8<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                             )<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                       )<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                 );<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  result = <\/span><span style=\"color: #001080\">expression<\/span><span style=\"color: #000000\">-&gt;<\/span><span style=\"color: #795E26\">Evaluate<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;Result of &quot;<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">expression<\/span><span style=\"color: #000000\">-&gt;<\/span><span style=\"color: #795E26\">Walk<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot; = &quot;<\/span><span style=\"color: #000000\"> &lt;&lt; result &lt;&lt; endl;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">delete<\/span><span style=\"color: #000000\"> expression;<\/span><span style=\"color: #008000\"> \/\/ Clean up expression AST<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">   \/\/ Another expression<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  expression =<\/span><span style=\"color: #008000\"> \/\/ AST represents: (53.5 - 45) * (73.54 + -3 + 8)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">           <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Times<\/span><span style=\"color: #000000\">(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                 <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Minus<\/span><span style=\"color: #000000\">(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                       <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Real<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">53.5<\/span><span style=\"color: #000000\">),<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                       <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Integer<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">45<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                       ),<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                 <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Plus<\/span><span style=\"color: #000000\">(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                       <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Real<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">73.54<\/span><span style=\"color: #000000\">),<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                       <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Plus<\/span><span style=\"color: #000000\">(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                             <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">UnaryMinus<\/span><span style=\"color: #000000\">(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                                   <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Integer<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">3<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                                   ),<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                             <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Integer<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">8<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                             )<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                       )<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                 );<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  result = <\/span><span style=\"color: #001080\">expression<\/span><span style=\"color: #000000\">-&gt;<\/span><span style=\"color: #795E26\">Evaluate<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;Result of &quot;<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">expression<\/span><span style=\"color: #000000\">-&gt;<\/span><span style=\"color: #795E26\">Walk<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot; = &quot;<\/span><span style=\"color: #000000\"> &lt;&lt; result &lt;&lt; endl;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">delete<\/span><span style=\"color: #000000\"> expression;<\/span><span style=\"color: #008000\"> \/\/ Clean up expression AST<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:40%\">\n<p>When you have completed the additional classes, compile this new&nbsp;<strong>syntax.cpp<\/strong>&nbsp;file and run it to get the results of the three expressions. (Cut the above code from this webpage and paste it into your editor.)<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>Note:&nbsp;<\/strong>You may notice that the second and third expressions in the above class are not correctly parenthesized when printed. Don&#8217;t worry about that for now; we&#8217;ll fix that later, in Part C.<\/p>\n<\/blockquote>\n\n\n\n<p>Note that, now, you&#8217;ll have three&nbsp;<strong>.cpp<\/strong>&nbsp;files to compile and link together.<\/p>\n\n\n\n<p><strong><em>What did you learn from this exercise?<\/em><\/strong><\/p>\n\n\n\n<p><strong><em>How hard was it to add these?<\/em><\/strong><\/p>\n<\/div>\n<\/div>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Part_C_Teaching_Walk_to_Correctly_Parenthesize\"><\/span><em>Part C: Teaching Walk() to Correctly Parenthesize<\/em><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>You probably noticed in Part B that there are certain expressions (such as the second and third expressions in our enhanced main program) where the output isn&#8217;t quite right &#8212; it needs extra parentheses to represent the correct operator precedence.<\/p>\n\n\n\n<p>Let&#8217;s fix this. Here are the steps we&#8217;ll follow:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Modify the&nbsp;<strong><code>Operator<\/code><\/strong>&nbsp;class as follows:\n<ul class=\"wp-block-list\">\n<li>Add a set of&nbsp;<strong><code>public const static<\/code>&nbsp;<\/strong>data members (why&nbsp;<strong><code>static<\/code><\/strong>&nbsp;and why&nbsp;<strong><code>const<\/code><\/strong>?) of type&nbsp;<strong><code>int<\/code><\/strong>, to indicate various operator precedence levels: <strong><code>PREC_HIGHEST<\/code>, <code>PREC_HIGHER<\/code>, <code>PREC_MEDIUM<\/code>, <code>PREC_LOWER<\/code>, <code>PREC_LOWEST<\/code><br><\/strong>Note that the actual values of these data members don&#8217;t matter, except that the values should represent the relative precedence values appropriately. I suggest that&nbsp;<strong><code>PREC_LOWEST<\/code><\/strong>&nbsp;be assigned a value of 0, and that you increase the value by 5 or 10 for each precedence level above that. That way, if you later needed to insert an additional precedence level, you have room to do so.<\/li>\n\n\n\n<li>Add a&nbsp;<strong><code>public<\/code>&nbsp;<\/strong>pure virtual member function&nbsp;<strong><code>Precedence()<\/code><\/strong>&nbsp;<br><strong>virtual int Precedence() const = 0;<\/strong><br>to force every class that is derived from the&nbsp;<strong><code>Operator<\/code><\/strong>&nbsp;class to implement this member function.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Once you&#8217;ve implemented all these&nbsp;<strong>Precedence()<\/strong>&nbsp;member functions, the entire project should build again. However, we haven&#8217;t actually used the member functions anywhere yet, so the behavior of our classes will still be as it was before.So, let&#8217;s finish the job:\n<ul class=\"wp-block-list\">\n<li>In each of the&nbsp;<strong><code>Walk()<\/code><\/strong>&nbsp;member functions, you&#8217;ll have to do the following:<\/li>\n\n\n\n<li>For each operand, find out whether the operand is an&nbsp;<strong><code>Operator<\/code><\/strong>. How?<br><em><strong>Hint:&nbsp;<\/strong>Take a look at the&nbsp;dynamic_cast&nbsp;operator.<\/em><\/li>\n\n\n\n<li>If it is an&nbsp;<strong><code>Operator<\/code><\/strong>, compare its precedence level with the precedence level of the current operator.<\/li>\n\n\n\n<li>If the precedence level of the operand operator is lower than that of the current operator, insert parentheses around the&nbsp;<strong><code>Walk()<\/code><\/strong>&nbsp;call for that operand.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>That should do it! Rebuild, and try it. Test, test, test!<\/p>\n\n\n\n<p>In my implementation, the second and third expressions printed out as:<\/p>\n\n\n\n<pre class=\"wp-block-code has-medium-font-size\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>Result of 53.5 \/ 45 - 73.54 * (+3 + 8) = -807.751\nResult of (53.5 - 45) * (73.54 + -3 + 8) = 667.59<\/strong><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Abstract Syntax Trees Description Various programs (such as compilers and calculators), use a data structure called an&nbsp;Abstract Syntax Tree (AST)&nbsp;to represent syntax that has been recognized by the program. NOTE: An Abstract Syntax Tree is&nbsp;not&nbsp;the same as a Binary Search Tree.&nbsp; In a Binary Search Tree, both interior and leaf nodes can contain data, and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":29,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-37","page","type-page","status-publish","hentry"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/37","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/comments?post=37"}],"version-history":[{"count":7,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/37\/revisions"}],"predecessor-version":[{"id":1706,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/37\/revisions\/1706"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/29"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/media?parent=37"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}